Wednesday, 15 May 2013

php - Replace All matching string only at start -


I want to change all matching events when the string starts. There is a usage case here: www.example.com/example.com/http://example.com/www.abc.com/www.example.com/xyz.com/example.com Should www.example.com / xyz.com / example.com

each match occurring must be removed to a non-matching string encounters .

I am currently using this regular expression preg_replace to me

'/ (http: \ / \ /) {0 The problem is that it replaces all the mailing strings. {0}

I just want to replace the matched stars in the beginning, if the match fails once, then it should not change anything, any suggestions to help?

Thanks

You can capture string instead of preg_replace:

  (? :(?: Www .http :? \ / \ /)? Example \ .com \ /) + (. *)   

the only capture In the group that string is included, see again.

In addition, I've found that your input string contained http :: and I got it in the rags with a ? With and last thing, {0}} to ? Can be written as . It is small :)

PHP code (I'm not very familiar with the language, but I believe this should work):

  preg_match ('/ (?: (?: Www .http:? \ / \ /)? Example \ .com \ /) + (. *) / ', $ Html, $ match); $ Url = $ match [1];    

No comments:

Post a Comment