Php regex expression replace

I have a line like this as a json response for a script:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit, HTTP://unknown/string/in/unknown/place/ , sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"

      

Is there a way to replace empty string using regex and php preg_replace?

I'm not familiar with regex, could you just give an example? I basically need to remove the substring starting with "http" and ending with a space if possible.

thank

+3


source to share


1 answer


Something like

preg_replace("/HTTP\S+\s,/", "","Lorem ipsum dolor sit amet, consectetur adipiscing elit, HTTP://unknown/string/in/unknown/place/ , sed do eiusmod tempor incididunt ut labore et dolore magna aliqua" );

      

Will give the result as

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

      

Regex /HTTP\S+\s,/

  • \S

    matches anything other than a space

  • \S

    matches a blank

Demo Regex



EDIT

Better solution like

/HTTP\S+(\s,|$)/

      

So the link can appear anywhere in the line, even at the end.

Example

preg_replace("/HTTP\S+(\s,|$)/", "","Lorem ipsum dolor sit amet, consectetur adipiscing elit, HTTP://unknown/string/in/unknown/place/" );
=> Lorem ipsum dolor sit amet, consectetur adipiscing elit,

      

Demo Regex

+5


source







All Articles