PHP preg_replace 所有文本都在更改
PHP preg_replace all text changing
我想对 html 进行一些更改,但我必须遵守某些规则。
我有这样的源代码;
A beautiful sentence http://www.google.com/test, You can reach here http://www.google.com/test-mi or http://www.google.com/test/aliveli
我需要将其转换为以下内容;
A beautiful sentence http://test.google.com/, You can reach here http://www.google.com/test-mi or http://test.google.com/aliveli
我尝试使用 str_replace;
$html = str_replace('://www.google.com/test','://test.google.com');
当我这样使用它时,我得到了一个不正确的结果,例如;
A beautiful sentence http://test.google.com/, You can reach here http://test.google.com/-mi or http://test.google.com/aliveli
错误替换: http://test.google.com/-mi
如何使用 preg_replace 执行此操作?
如果句子是您问题中的唯一情况,则您无需开始纠结于 preg_replace
。
只需将您的 str_replace()
函数调用更改为以下内容(在搜索字符串部分末尾带有“,”):
$html = str_replace('://www.google.com/test,','://test.google.com/,');
这与所需搜索参数的第一次出现相匹配,对于目标句子中的最后一个参数,添加此(请注意末尾的“/”):
$html = str_replace('://www.google.com/test/','://test.google.com/');
更新:
使用这两个:
$targetStr = preg_replace("/:\/\/www.google.com\/test[\s\/]/", "://test.google.com/", $targetStr);
它将匹配除末尾带逗号的所有内容。对于那些,您应该使用以下内容:
$targetStr = preg_replace("/:\/\/www.google.com\/test,/", "://test.google.com/,", $targetStr);
您似乎正在将子目录 test
替换为子域。你的案子好像太复杂了。但是我已经尽力应用一些可能可靠或可能不可靠的逻辑,除非您的字符串保持相同的结构。但是你可以试试这个代码:
$html = "A beautiful sentence http://www.google.com/test, You can reach here http://www.google.com/test-mi or http://www.google.com/test/aliveli";
function set_subdomain_string($html, $subdomain_word) {
$html = explode(' ', $html);
foreach($html as &$value) {
$parse_html = parse_url($value);
if(count($parse_html) > 1) {
$path = preg_replace('/[^0-9a-zA-Z\/-_]/', '', $parse_html['path']);
preg_match('/[^0-9a-zA-Z\/-_]/', $parse_html['path'], $match);
if(preg_match_all('/(test$|test\/)/', $path)) {
$path = preg_replace('/(test$|test\/)/', '', $path);
$host = preg_replace('/www/', 'test', $parse_html['host']);
$parse_html['host'] = $host;
if(!empty($match)) {
$parse_html['path'] = $path . $match[0];
} else {
$parse_html['path'] = $path;
}
unset($parse_html['scheme']);
$url_string = "http://" . implode('', $parse_html);
$value = $url_string;
}
}
unset($value);
}
$html = implode(' ', $html);
return $html;
}
echo "<p>{$html}</p>";
$modified_html = set_subdomain_string($html, 'test');
echo "<p>{$modified_html}</p>";
希望对您有所帮助。
使用正则表达式,您可以使用 word boundary and a lookahead 来防止在 -
处进行替换
$pattern = '~://www\.google\.com/test\b(?!-)~';
$html = preg_replace($pattern, "://test.google.com", $html);
这是一个regex demo at regex101 and a php demo at eval.in
请注意,您需要使用反斜杠转义 certain characters,因为它的特殊含义是在使用正则表达式时逐字匹配它们。
我想对 html 进行一些更改,但我必须遵守某些规则。
我有这样的源代码;
A beautiful sentence http://www.google.com/test, You can reach here http://www.google.com/test-mi or http://www.google.com/test/aliveli
我需要将其转换为以下内容;
A beautiful sentence http://test.google.com/, You can reach here http://www.google.com/test-mi or http://test.google.com/aliveli
我尝试使用 str_replace;
$html = str_replace('://www.google.com/test','://test.google.com');
当我这样使用它时,我得到了一个不正确的结果,例如;
A beautiful sentence http://test.google.com/, You can reach here http://test.google.com/-mi or http://test.google.com/aliveli
错误替换: http://test.google.com/-mi
如何使用 preg_replace 执行此操作?
如果句子是您问题中的唯一情况,则您无需开始纠结于 preg_replace
。
只需将您的 str_replace()
函数调用更改为以下内容(在搜索字符串部分末尾带有“,”):
$html = str_replace('://www.google.com/test,','://test.google.com/,');
这与所需搜索参数的第一次出现相匹配,对于目标句子中的最后一个参数,添加此(请注意末尾的“/”):
$html = str_replace('://www.google.com/test/','://test.google.com/');
更新:
使用这两个:
$targetStr = preg_replace("/:\/\/www.google.com\/test[\s\/]/", "://test.google.com/", $targetStr);
它将匹配除末尾带逗号的所有内容。对于那些,您应该使用以下内容:
$targetStr = preg_replace("/:\/\/www.google.com\/test,/", "://test.google.com/,", $targetStr);
您似乎正在将子目录 test
替换为子域。你的案子好像太复杂了。但是我已经尽力应用一些可能可靠或可能不可靠的逻辑,除非您的字符串保持相同的结构。但是你可以试试这个代码:
$html = "A beautiful sentence http://www.google.com/test, You can reach here http://www.google.com/test-mi or http://www.google.com/test/aliveli";
function set_subdomain_string($html, $subdomain_word) {
$html = explode(' ', $html);
foreach($html as &$value) {
$parse_html = parse_url($value);
if(count($parse_html) > 1) {
$path = preg_replace('/[^0-9a-zA-Z\/-_]/', '', $parse_html['path']);
preg_match('/[^0-9a-zA-Z\/-_]/', $parse_html['path'], $match);
if(preg_match_all('/(test$|test\/)/', $path)) {
$path = preg_replace('/(test$|test\/)/', '', $path);
$host = preg_replace('/www/', 'test', $parse_html['host']);
$parse_html['host'] = $host;
if(!empty($match)) {
$parse_html['path'] = $path . $match[0];
} else {
$parse_html['path'] = $path;
}
unset($parse_html['scheme']);
$url_string = "http://" . implode('', $parse_html);
$value = $url_string;
}
}
unset($value);
}
$html = implode(' ', $html);
return $html;
}
echo "<p>{$html}</p>";
$modified_html = set_subdomain_string($html, 'test');
echo "<p>{$modified_html}</p>";
希望对您有所帮助。
使用正则表达式,您可以使用 word boundary and a lookahead 来防止在 -
$pattern = '~://www\.google\.com/test\b(?!-)~';
$html = preg_replace($pattern, "://test.google.com", $html);
这是一个regex demo at regex101 and a php demo at eval.in
请注意,您需要使用反斜杠转义 certain characters,因为它的特殊含义是在使用正则表达式时逐字匹配它们。