为简洁起见...
我想从字符串中取出项目,将它们放入单独的数组中,用 ID 标记替换从字符串中提取的值,解析字符串,然后将提取的项目放回它们的原始位置(按正确顺序)。(如果这是有道理的,那么跳过其余的:D)
我有以下字符串;
“我的句子包含指向 [url] 和 [url] 的 URL,这让我的生活变得困难。”
由于各种原因,我想删除这些 URL。但我需要保留它们的位置,稍后再重新插入它们(在处理完字符串的其余部分之后)。
因此我愿意;
“我的句子包含指向 [url] 和 [url] 的 URL,这让我的生活变得困难。”
成为;
“我的句子包含指向 [token1fortheURL] 和 [token2fortheURL] 的 URL,这让我的生活变得困难。”
我已经尝试过几次,各种方式。我所做的就是撞砖墙并发明新的脏话!
我使用以下代码进行设置;
$mystring = 'my sentence contains URLs to http://www.google.com/this.html and http://www.yahoo.com which makes my life difficult.';
$myregex = '/(((?:https?|ftps?)\:\/\/)?([a-zA-Z0-9:]*[@])?([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}|([0-9]+))([a-zA-Z0-9-._?,\'\/\+&%\$#\=~:]+)?)/';
$myextractions = array();
然后我做一个 preg_replace_callback;
$matches = preg_replace_callback($myregex,'myfunction',$mystring);
我的功能如下;
function myfunction ($matches) {}
正是在这里,砖墙开始发生。我可以将东西放入空白提取数组中 - 但它们在函数之外不可用。我可以使用令牌更新字符串,但我无法访问被替换的 URL。我似乎无法向 preg_replace_callback 中的函数调用添加其他值。
我希望有人可以提供帮助,因为这让我发疯。
更新:
根据@Lepidosteus 建议的解决方案,我认为我有以下工作?
$mystring = 'my sentence contains URLs to http://www.google.com/this.html and http://www.yahoo.com which makes my life difficult.';
$myregex = '/(((?:https?|ftps?)\:\/\/)?([a-zA-Z0-9:]*[@])?([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}|([0-9]+))([a-zA-Z0-9-._?,\'\/\+&%\$#\=~:]+)?)/';
$tokenstart = ":URL:";
$tokenend = ":";
function extraction ($myregex, $mystring, $mymatches, $tokenstart, $tokenend) {
$test1 = preg_match_all($myregex,$mystring,$mymatches);
$mymatches = array_slice($mymatches, 0, 1);
$thematches = array();
foreach ($mymatches as $match) {
foreach ($match as $key=>$match2) {
$thematches[] = array($match2, $tokenstart.$key.$tokenend);
}
}
return $thematches;
}
$matches = extraction ($myregex, $mystring, $mymatches, $tokenstart, $tokenend);
echo "1) ".$mystring."<br/>";
// 1) my sentence contains URLs to http://www.google.com/this.html and http://www.yahoo.com which makes my life difficult.
function substitute($matches,$mystring) {
foreach ($matches as $match) {
$mystring = str_replace($match[0], $match[1], $mystring);
}
return $mystring;
}
$mystring = substitute($matches,$mystring);
echo "2) ".$mystring."<br/>";
// 2) my sentence contains URLs to :URL:0: and :URL:1: which makes my life difficult.
function reinsert($matches,$mystring) {
foreach ($matches as $match) {
$mystring = str_replace($match[1], $match[0], $mystring);
}
return $mystring;
}
$mystring = reinsert($matches,$mystring);
echo "3) ".$mystring."<br/>";
// 3) my sentence contains URLs to http://www.google.com/this.html and http://www.yahoo.com which makes my life difficult.
这似乎有效?