如果我在一个字符串中有这个标签:[phpbay]{keyword}, 30, "", "", "", "", "", "", "", "", "", "", "", "", "2", "", "", ""[/phpbay]
我可以删除它和标签之间的内容吗?也许用正则表达式?谢谢!
preg_replace('~'[phpbay'](.+?)'[/phpbay']~','',$string);
$string = "[phpbay]blah blah blah [/phpbay]";
$stripped_string = preg_replace('~'['/?phpbay']~', '', $string);
如果我没看错你的问题,并且只希望保留"blah blah blah"部分
这个正则表达式将给出这两个标签之间的所有内容:
'[phpbay](.+)'[/phpbay]
是的,这里可以使用正则表达式:
$str = preg_replace('#'[phpbay][{'w},'s'd"]+'[/phpbay]#', "", $str);
这只是从字符串中删除"标签",如果它只包含你的例子中的字符。如果您只想删除它,如果它包含例如您的示例30
或特定的{keyword}
,您将不得不使regex更具体。
请参见https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world获取一些可能有帮助的工具。
我个人会使用str_replace或str_ireplace(不区分大小写)
$Find = array("[phpbay]","[/phpbay]");
$Replace = array("<b>","</b>");
$Source = "[phpbay]Hello World[/phpbay]";
//Couldn't get any simpler...Replace [phpbay]Content[/phpbay] with <b>Content</b>
echo str_ireplace($Find,$Replace,$Source);
经过尝试和测试,它有效。
希望这对你有帮助!