我有一个字符串,它包含:
[quote name="username" url="/t/44223/topics-name#post_734738"]我想要编写一个php regex,它将查找这个字符串的所有出现,并找到post编号(ex )。( 734738)然后用以下文字替换整个事物:
[quote=username;new post number based on 734738]要求:
请帮帮我。谢谢。
最后答案:
如果有人需要这样做,这是最后的答案:)
$string = '[quote name="user343I_nsdfdame" url="/t/44223/topics-name#post_734738"]What is your name?[/quote][quote name="username" url="/t/45454/topics-name#post_767676"]Test string[/quote]The quick brown fox....';
if(preg_match_all("/\[quote name=\"([^\"]*)\" url=\"\/t\/[^#]*#post_([0-9]*)\"\]/", $string, $matches)) {
foreach ($matches[0] as $match) {
if(preg_match("/\[quote name=\"([^\"]*)\" url=\"\/t\/[^#]*#post_([0-9]*)\"\]/",$match, $reg)) {
$new = "[quote=".$reg[1].";".$reg[2]."]"; // I have changed $reg[2] according to my need.
$string = str_replace($match, $new, $string);
}
}
}
echo $string;输出:
[quote=user343I_nsdfdame;734738]What is your name?[/quote][quote=username;767676]Test string[/quote]The quick brown fox....发布于 2015-08-23 18:20:51
这样可以做到:
$string = '[quote name="username" url="/t/44223/topics-name#post_734738"]';
$new = "";
if(preg_match("/\[quote name=\"([^\"]*)\" url=\"\/t\/[^#]*#post_([0-9]*)\"\]/",$string,$reg)) {
$new = "[quote=".$reg[1].";new post number based on ".$reg[2]."]";
}
echo $new;产出如下:
[quote=username;new post number based on 734738]https://stackoverflow.com/questions/32169729
复制相似问题