Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅替换链接内部的字符串_Php_String - Fatal编程技术网

Php 仅替换链接内部的字符串

Php 仅替换链接内部的字符串,php,string,Php,String,我有这样一个字符串: <a href="blabla/test/city">city</a> 我只想删除实际链接中最后一次出现的/city,我将有: <a href="blabla/test">city</a> 我不能只做替换,因为我不想替换浏览器中显示的城市 我开始做一些事情: $x = '<a href="test/gothenburg">gothenburg</a>'; $pos = strrpos

我有这样一个字符串:

<a href="blabla/test/city">city</a>

我只想删除实际链接中最后一次出现的/city,我将有:

<a href="blabla/test">city</a>

我不能只做替换,因为我不想替换浏览器中显示的城市

我开始做一些事情:

$x = '<a href="test/gothenburg">gothenburg</a>';    
$pos = strrpos($x, '">');
$x = substr($x, 0, $pos);                        
echo $x;
$x='';
$pos=strrpos($x,“>”);
$x=子项($x,0,$pos);
echo$x;
如何安全地实现此替换

strreplace(Href="blabla/test/city", href="blabla/test")

使用真正的str替换当然

您可以使用
preg\u替换

$searchText = '<a href="blabla/test/city">city</a>';

$result = preg_replace("/(\/\w+)(?=[\"])/u", "", $searchText);

print_r($result);
$searchText='';
$result=preg\u replace(“/(\/\w+(=[\“]))/u“,”,$searchText);
打印(结果);
输出:

<a href="blabla/test">city</a>

例如:

要将
/
保留在替换的单词之前,可以使用模式:
(\w+(=[“])

提出了一个使用and的解决方案,该解决方案将在
标记之间查找城市名称,然后找到
href
并删除包含城市名称的URL的最后一部分:

// Set the city link HTML.
$city_link_html = '<a href="test/gothenburg/">gothenburg</a>';

// Run a regex to get the value between the link tags.
preg_match('/(?<=>).*?(?=<\/a>)/is', $city_link_html, $city_matches);

// Run the preg match all command with the regex pattern.
preg_match('/(?<=href=")[^\s"]+/is', $city_link_html, $city_url_matches);

// Set the new city URL by removing only the the matched city from the URL.
$new_city_url = preg_replace('#' . $city_matches[0] . '?/$#', '', $city_url_matches[0]);

// Replace the old city URL in the string with the new city URL.
$city_link_html = preg_replace('#' . $city_url_matches[0] . '#', $new_city_url, $city_link_html);

// Echo the results with 'htmlentities' so the results can be read in a browser.
echo htmlentities($city_link_html);
//设置城市链接HTML。
$city_link_html='';
//运行正则表达式以获取链接标记之间的值。
preg_match('/(?).*(?=)/is',$city_link_html,$city_matches);
//使用regex模式运行preg match all命令。

preg_match('/(?路径中不包含“city”的内容,那么您将遇到问题。我确实在末尾得到了尾随斜杠。我不知道为什么。我的问题不够清楚。是否有方法仅替换字符串中的最后一个链接?(如果同一字符串中有多个链接)@bestprogrammerintheworld这可以删除
href
中的最后一项,但我发布了一个答案,在
标记之间检查城市名称,然后找到
href
并删除包含城市名称的URL的最后一部分。解释会很好。@最后一场比赛,不确定有多少竞争在这段简短的代码中可以给出的解释。
// Set the city link HTML.
$city_link_html = '<a href="test/gothenburg/">gothenburg</a>';

// Run a regex to get the value between the link tags.
preg_match('/(?<=>).*?(?=<\/a>)/is', $city_link_html, $city_matches);

// Run the preg match all command with the regex pattern.
preg_match('/(?<=href=")[^\s"]+/is', $city_link_html, $city_url_matches);

// Set the new city URL by removing only the the matched city from the URL.
$new_city_url = preg_replace('#' . $city_matches[0] . '?/$#', '', $city_url_matches[0]);

// Replace the old city URL in the string with the new city URL.
$city_link_html = preg_replace('#' . $city_url_matches[0] . '#', $new_city_url, $city_link_html);

// Echo the results with 'htmlentities' so the results can be read in a browser.
echo htmlentities($city_link_html);
<a href="test/">gothenburg</a>