Hello everyone, in php is not very strong - so I do not know how to properly describe the problem.
Therefore, immediately for example:

search.php?q=free-online-film-view 

It is necessary that the free-online-film-view , turned so

 search.php?q=free-online-film search.php?q=free-online search.php?q=free 

That is, that the word is divided to a minimum and that the view is such

 <ul> <li><a href="search.php?q=free-online-film">free online film</a></li> <li><a href="search.php?q=free-online">free online</a></li> <li><a href="search.php?q=free">free</a></li> </ul> 

thank

  • one
    @jikol; To format a code, select it with the mouse and click on the {} button of the editor. - ReinRaus

2 answers 2

I can offer this option :

 $str = 'search.php?q=free-online-film-view'; $main_parts = explode('=',$str); $vals = explode('-',$main_parts[1]); for($i = 0; $i <= count($vals); $i++){ array_pop($vals); echo $main_parts[0].'='.implode('-',$vals).'<br>'; } 

You can still use the option, using strrpos () , finding the last occurrence of the character "-" in the line and cutting the line before it. But in my opinion - this is a more hemorrhoidal method.

  • The script works, I corrected it a little for myself. Here echo '<li> <a href="'.$main_parts[00.'='.implode('-' ,$vals).'"> '.' ' .implode ('', $ vals). ''. '</a> </ li>'; But there is a joint that I don’t know how to solve. If we have $ str = 'search.php? Q = free'; then what comes out here is <a href="search.php?q="> </a> how to decide that it doesn't show up if the words do not divide - jikol
  • You can do the check: if (count ($ vals)> 1) {for ($ i = 0; $ i <= count ($ vals); $ i ++) {echo $ main_parts [0]. '='. Implode (' - ', $ vals).' <br> '; array_pop ($ vals); }} else {// output the original string} - Deonis
  • It worked, but when requested, search.php? Q = free-online gives the following: <li> <a href="search.php?q=free-online"> free online </a> </ li> <li> < a href = "search.php? q ="> </a> </ li> - jikol
  • Well, so why not immediately indicate in the question that the number of such parameters can vary from 1 to N? Or are there any other nuances? [See link] [1]. [1]: phpfiddle.org/main/code/xbi-qr3 - Deonis
  • one
    Or even [in this way] [1] you can: [1]: phpfiddle.org/main/code/7gm-wu3 - Deonis

I can offer a slightly different option:

 $str = "free-online-film-view"; $tags = explode("-", $str); $i=count($tags); foreach($tags as $tag){ echo "li - ". implode('-', array_slice($tags,0, $i--))."\n"; } 

Code on ideone.com