0

I have a string that is generated via a function.

$string = function();

It generates something like:

$string = '<ul><li><a href="">Test</a>(10)</li>';

My question is, how do I move (10) part into the end of the anchor tag, so we have:

$string = '<ul><li><a href="">Test (10)</a></li>';

I want to do this to all anchor tags in the list items.

What's the appropriate PHP approach?

Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
  • 4
    Change the function. – Niet the Dark Absol Jul 11 '17 at 10:19
  • do changes in your function itself.share that function here, may be you get help – Alive to die - Anant Jul 11 '17 at 10:20
  • 1
    @NiettheDarkAbsol I can't change the function. It is generated by a core WordPress function. – Henrik Petterson Jul 11 '17 at 10:21
  • I'm pretty sure OP would change the function if he could. =D – Gary Woods Jul 11 '17 at 10:22
  • You can use [PHP's convenient string functions](http://php.net/manual/en/ref.strings.php). How about you try to toss up a function, see if you can get it to work. If not, show us your approach and tell us what doesn't work. – domsson Jul 11 '17 at 10:23
  • I haven't used WordPress, but I've used other systems and I'm pretty sure you *can* customise core functions, although you'll need to make notes and patch them again when you update the software (in my case, MediaWiki) – Niet the Dark Absol Jul 11 '17 at 10:24
  • @NiettheDarkAbsol It's easy for me to change the core files of WP, but this is client work and it's unethical because things will break for them as soon as they update. :) – Henrik Petterson Jul 11 '17 at 10:25
  • @domdom Please post an answer demonstrating this. – Henrik Petterson Jul 11 '17 at 10:25
  • My point was that you could probably come up with such code yourself instead of asking us to write it. But it doesn't matter now, Mahipal already provided you with a solution. :) – domsson Jul 11 '17 at 10:29
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – user3942918 Jul 11 '17 at 10:37

2 Answers2

3

Just use srt_replace function for your string variable like below:

if(strpos($string,"</a>")) {
    $string = str_replace('</a>',' ', $string);
    // output <ul><li><a href="">Test'(10)</li>
    echo $string = str_replace('</li>','</a></li>', $string);
    // output <ul><li><a href="">Test (10)</a></li>
}
Mahipal Patel
  • 543
  • 3
  • 15
  • 1
    This is a *very* interesting approach. Is there any way I can make a "check" before I do the `str_replace()`? There are a few `
  • ` items that doesn't have anchor tags inside them.
  • – Henrik Petterson Jul 11 '17 at 10:27
  • This will break unless if **all** of your list tags have a tags inside them. There needs to be a check prior to running the str_replace function... – Gary Woods Jul 11 '17 at 10:32
  • 1
    yes you can pre check if anchor is exist using strpos() or strstr() functions check http://php.net/manual/en/function.strpos.php if position found then perform replace function other just skip it. – Mahipal Patel Jul 11 '17 at 10:44
  • 1
    Can you kindly update your answer with the strpos() or strstr() approach? – Henrik Petterson Jul 11 '17 at 10:50
  • please approve answer if this will help u. – Mahipal Patel Aug 05 '17 at 09:55