开发者

PHP PREG_REPLACE changing string

开发者 https://www.devze.com 2023-03-27 16:48 出处:网络
I have the following String: \"Something:string(lower-case) anything Somethingb:numbers开发者_高级运维\"

I have the following String:

"Something:string(lower-case) anything Somethingb:numbers开发者_高级运维"

I am trying to get the char after Something and the numbers after Somethingb with preg_replace.

Also, sometimes the string will be just the first part:

"Something char anything"

So what I need is an outcome like this using preg_replace:

"string(lowercase) , numbers"

or just

"string(lowercase)"

if the Something b is not in the string.

Any idea would be much appreciated.

Thanks


Here is regex for first case:

/Something:([a-z]+).*?Somethingb:(\d+)/

and replace

$1 , $2

also you can pass &$count parameter to know if replace was successful

In second case do you want "Something char anything" to become "something char anything"? if this is so that you can just do strtolower($string) if $count == 0

0

精彩评论

暂无评论...
验证码 换一张
取 消