0

Trying to remove text from between </span> and </a>. I want to just remove the some text here just after the </span> so that it only shows the icon.

$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. It won't strip out the text after the </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;
    }
?>

enter image description here

2 Answers2

1

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:

<?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 /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>
PKeidel
  • 2,559
  • 1
  • 21
  • 29
  • I ended up going this route. Was simpler and cleaner. I update the regex to account for more than 1 URL set. I'll update my code above with the final result. –  Nov 22 '16 at 19:42
  • I added an additional block of code to my question, trying to incorporate your function. This method makes the most sense to me and is a cleaner approach. But apparently I'm not implementing this correctly. Thoughts? –  Dec 02 '16 at 21:31
  • @JonnyBorg try removing the `?` from the regex. So that it is `.*?<\/a>` – PKeidel Dec 08 '16 at 21:21
  • Still having issues. So, I refined my question to be more clear and around using Regex as I think this is a cleaner approach. –  Dec 16 '16 at 21:37
  • I also edited my answer to show my tests. There linkedin works. – PKeidel Dec 20 '16 at 22:13
0

Obligatory https://stackoverflow.com/a/1732454/2191572 but this should work.

If I may re-word your requirements then you want to remove stuff in between </span> and </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;
Community
  • 1
  • 1
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Correct. That is exactly what I'm after, but the href's for some reason are getting stripped too. –  Nov 21 '16 at 17:53
  • @JonnyBorg even using my solution? – MonkeyZeus Nov 21 '16 at 17:53
  • @JonnyBorg Could you update your question with the exact `$bad_string` which you are trying to parse? Make sure to point out if there are newlines `\n` or anything because if you have multiple URLs then additional pre-processing will have to be performed – MonkeyZeus Nov 21 '16 at 17:56
  • @JonnyBorg Any luck with this? – MonkeyZeus Nov 21 '16 at 19:26