简体   繁体   中英

php regular expression string replace

I have a string like that as 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 replacewith an empty string using regular expression and php preg_replace?

I'm not familiar with regular expression, could you just provide an example? I basically need to remove the substring starting with 'http' and finishing with a space, IF possible.

thanks

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 output 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 space

Regex Demo

EDIT

A better solution like

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

So that the link can apear anywhere in the string 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,

Regex Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM