简体   繁体   English

删除之间的文字 <a></a> 但不是href或 <span></span> 使用PHP

[英]Remove Text between <a></a> but not the href or <span></span> using PHP

Trying to remove text from between </span> and </a> . 尝试从</span></a>之间删除文本。 I want to just remove the some text here just after the </span> so that it only shows the icon. 我只想在</span>之后删除some text heresome text here以便仅显示图标。

$bad_string = '<li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>';

$good_string = '<li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>';

What currently echoes: 当前回声:

<ul id="menu-social-menu-1" class="menu">
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span>some text here</a></li>
</ul>

What should echo: 应该回声什么:

<ul id="menu-social-menu-1" class="menu">
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
    <li class="someclasses"><a target="_blank" href="http:www.example.com/dir/handle"><span></span></a></li>
</ul>

Using this below code in WordPress the text after the span is still present. 在WordPress中使用以下代码,跨度后的文本仍然存在。 It won't strip out the text after the </span> . 不会在</span>之后删除文本。 What's my mistake? 我怎么了 here: 这里:

<?php if ( has_nav_menu( 'social-menu' ) ) {
    $menu = wp_nav_menu( array( 'theme_location' => 'social-menu', 'fallback_cb' => '', 'echo' => false ) );

    function convertURL($menu) {
        return preg_replace("/<a target=\"_blank\" href=\"([^\"]+?)\"><span><\/span>.*?<\/a>/", "<a target=\"_blank\" href=\"$1\"><span></span></a>", $menu);
    }

    echo $menu;
    }
?>

在此处输入图片说明

Why don't you just solve that with a regex? 您为什么不只用正则表达式解决呢?

function convertURL($url) {
  return preg_replace("/<a href=\"([^\"]+?)\">.*/", "<a href=\"$1\"><span></span></a>", $url);
}
// input:  <a href="http:www.example.com/dir/handle"><span></span>some text here</a>
// output: <a href="http:www.example.com/dir/handle"><span></span></a>

EDIT: 编辑:
test.php: test.php:

<?php
function convertURL($url) {
  return preg_replace("/<a href=\"([^\"]+?)\">.*/", "<a href=\"$1\"><span></span></a>", $url);
}

foreach(['<a href="http://linkedin.com/some/vanity"><span></span>some text here</a>', '<a href="http:www.example.com/dir/handle"><span></span>some text here</a>'] as $url) {
  echo "$url => ";
  echo convertURL($url)."\n";
}

php test.php: php test.php:

$ php /tmp/test.php
<a href="http://linkedin.com/some/vanity"><span></span>some text here</a> => <a href="http://linkedin.com/some/vanity"><span></span></a>
<a href="http:www.example.com/dir/handle"><span></span>some text here</a> => <a href="http:www.example.com/dir/handle"><span></span></a>

Obligatory https://stackoverflow.com/a/1732454/2191572 but this should work. 必须使用https://stackoverflow.com/a/1732454/2191572,但这应该可以工作。

If I may re-word your requirements then you want to remove stuff in between </span> and </a> 如果我可以重新提出您的要求,那么您想删除</span></a>

$bad_string = '<a href="http:www.example.com/dir/handle"><span>social icon</span>some text here</a>';

$parts = explode('</span>', $bad_string); // explode on </span> and save the array
array_pop($parts); // remove last element of array which should be "some text here</a>"
$parts[] = '</a>'; // re-add the </a>

$good_string = implode('</span>', $parts); // re-join the array on </span>

echo $good_string;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM