简体   繁体   中英

regexp in php doesn't work for @\\section{(.*?)}@U

I wrote a simple regex-script in PHP:

<?php  
$string = "\section{Test 1} 
sdfgsdfg";  
$regex = "@\\section{(.*?)}@U";  
$replace = "$1jksdfahlkjh";  
$newString = preg_replace ($regex, $replace, $string, -1 );  
?> 

See: http://regexp-tester.mediacix.de/t287

using

echo $newString;

only give me

\\section{Test 1} sdfgsdfg

Regex-patterns like

$bbcode = array(

"/\[b\](.*?)\[\/b\]/is" => "<strong>$1</strong>",
"/\[u\](.*?)\[\/u\]/is" => "<u>$1</u>",
"/\[url\=(.*?)\](.*?)\[\/b\]/is" => "<a href='$1'>$2</a>"

);
$text = "[b]Text[/b][u]Text[/u]";

$text = preg_replace(array_keys($bbcode), array_values($bbcode), $text);
echo $text;

works great. Is there any solution? I don't want to write an whole LaTeX-Parser, but I want to replace those "\\section{...}"-Strings

You need to escape the \\ 's, because otherwise they will already be treated as an escape by php itself and then stripped:

$regexp = "@\\\\section{(.*)}@";

The U modifier is unnecessary, as that makes the .*? greedy again, so you are reversing the reversing. Just .* works just as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM