PHP remove everything after the last instance of a string

by 5 replies
6
I did the first instance with preg_replace, tried to loop it with while but it timed out even with a break escape.

Any ideas?

Rick
#programming #instance #php #remove #string
  • Got it


    $search_remove = strripos($search,"My string");


    $search = substr($search, $search_remove);


    echo $search;

    Then I'll just remove the final string
    • [ 1 ] Thanks
  • [DELETED]
  • substr and strrpos would be useful

    $until = substr($string, 0, strrpos($string.",", ","));

    #programming #code #php
    • [ 1 ] Thanks
    • [1] reply
    • You guys are so much better at doing stuff on one line than I am. I appreciate all the help.

      Rick
  • It's an older post, but I thought I'd mention - you seem to be using this code for a search the user can enter. If so, by echoing it unchanged you risk all sorts of problems, since the code is 'live' (it can be run by the browser), like javascript or html.

    So before you echo do this at a minimum:

    echo htmlentities($search,ENT_QUOTES);

    The command htmlentities() turns entered 'live' html into safe code - for example <img> becomes &lt;img&gt; which displays properly, instead of trying to load an image.
    • [ 1 ] Thanks
    • [1] reply
    • Woe that one's new to me thank you

      Rick

Next Topics on Trending Feed