php - The str_ireplace and preg_replace attemt? -


I am trying to replace a string with one original text. ( zinc -> gt; zn ) Example: 'zinc zinc zinc zinc 3! Zinc Zodiac # Zinc Zinc 9 Zinc @ zinc @ want to: 'zn zn zn zinc3! Zn zincmatic #zinc zinc9 zn @zinc @ stack_replayed attempts: $ text = 'zinc zinc zinc zinc 3 ! Zinc zodiac # zinc zinc 9 zinc @ zinc @ '; $ Word = 'zinc'; $ Attr = 'zn'; // cnt as str_ireplace will now be converted to zinc znmatic and # zinc #zn $ text = ''. $ Text will change in ''; $ Word = ''. $ Zinc ''; // will now try $ result = str_ireplace ($ word, $ attr, $ word); Echo trim ($ result);

Print zn zinc zn zinc3! Zinc zinc # zinc zinc 9 zn @ zinc @ .

Preg_replace attempts: P>

  $ text = 'zinc zinc Zinc zinc 3! Zinc zodiac # zinc zinc 9 zinc '; $ Word = 'zinc'; $ Attr = 'zn'; $ Result = preg_replace ("/ \ b ($ word) \ b / i", $ attr, $ text); $ Return result;   

Print zn zn zn zinc3! Zn zincmatic #zn zinc9 zn @zn @ Almost what I want: It seems that in zinc, zn even if some special character near ! Zinc is or # zonx but if any number is zinc9 or junkmic like text

I just have one I want to put the rule here so that # zinc is passed to # zink , @ zinc @ to @zink_ and ! Zinc to ! Zn changes into

What to add is the exception of special characters if zinc is near one of them (i.e.: #zinc , Zinc # < / Code>, zinc@ , @ zanx )

I want to be an executable, the letter is # , and , @

Thanks!

You can define such exceptions with negative \ b Behave like and can actually be used in conjunction.

In your case you want (? & Lt;! [# & Amp; @]) to check the previous character, and (?! [ # & Amp; @]) Click .

  = preg_replace ("/ (? & Lt;?! [# & Amp; @]!) \ B ($ word) \ b ([# & amp; @]) / I ", $ Attr, $ text);    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -