js下正则是这样的:
<code>var str = "data\/ui_inner\/74\/"; str.replace(/\\/,""); </code>
php如下这样写却不对:
<code>$auto_url = preg_replace('/\\/','', $str); </code>
我的目的是替换字符串中的\为空,请问如何替换呢?
按照楼下的答案试了试还是不行呢。。
找到原因了,是因为json_encode()导致的,请问如何解决呢?我去google一下
回复内容:
js下正则是这样的:
<code>var str = "data\/ui_inner\/74\/"; str.replace(/\\/,""); </code>
php如下这样写却不对:
<code>$auto_url = preg_replace('/\\/','', $str); </code>
我的目的是替换字符串中的\为空,请问如何替换呢?
按照楼下的答案试了试还是不行呢。。
找到原因了,是因为json_encode()导致的,请问如何解决呢?我去google一下
除非楼主有洁癖, 否则不建议再去对 json_encode
的结果进行替换, 因为 json_encode
对 /
进行转义输出, 对于js来说, 它的内容 还是 /
, 而不是 \/
.
(PS:下面在测试的时候有注释代码里的var_dump
, 只保留echo
)
替换的:
不替换的:
1.html
的代码如下:
<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript"> $.ajax({ type: "get", url: "f.php", dataType: "json", success: function(data) { console.log(data.url); } }); </script>
PHP中提供了一个函数 preg_quote
<code>string preg_quote ( string $str [, string $delimiter = NULL ] ) </code>
preg_quote()需要参数 str 并向其中 每个正则表达式语法中的字符前增加一个反斜线。 这通常用于你有一些运行时字符串 需要作为正则表达式进行匹配的时候。
正则表达式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! | : -
<code><?php $keywords = '$40 for a g3/400'; $keywords = preg_quote($keywords, '/'); echo $keywords; // 返回 \$40 for a g3\/400 </code></code>
所以以上代码可以这样写
<code><?php $str = "data\/ui_inner\/74\/"; $pattern = preg_quote('\\'); $auto_url = preg_replace('/'.$pattern.'/','', $str); echo $auto_url;</code></code>
应该这么写
<code>$auto_url = preg_replace('/\\\\/','', $str);</code>
也是刚学到,因为在字符串中,'\'也是转义,'/\\/'就变成了'/\/',所以要四个。
也可以用str_replace
<code>$auto_url = str_replace('\\','', $str);</code>
PHP的单引号中仍然会转义\
所以你需要这么做
$auto_url = preg_replace('/\\\\/', '', "data\/ui_inner\/74\/");
改成四个\\\\
直接对json做正则替换是不安全的,因为如果业务逻辑有改变,你可能意外修改了其他的域,建议先decode之后修改好之后再重新encode

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
