How to exclude alt using PHP regular expressions

Source: Internet
Author: User
Tags php regular expression
PHP regular internal chain exclusion alt method PHP now needs to implement the internal chain function, replace the keyword with the tag, but alt also has a keyword replacement, it will cause an error,

I want to replace the keywords in alt with other characters and then replace them

$ Newsinfo [content] = preg_replace ("/(? <= Alt = \ ". *) (combat )(? =. * \ ")/", "& % ","
However, it seems that this write is not matched. I have no problem in the regular expression tool testing. Is there a problem with the regular expression I wrote? Is there any way to implement it? thank you.


Reply to discussion (solution)

Php (? <= Pattern) assertions are fixed-length
What are you about is not long (? <= Alt = \".*)

Echo preg_replace ('/alt = ". +? (Combat). +? "/'," & % ",'');

Echo preg_replace ('/alt = ". +? (Combat). +? "/'," & % ",'');


In this case, replace the entire alt. I just want to replace the keywords contained in alt.

Php (? <= Pattern) assertions are fixed-length
What are you about is not long (? <= Alt = \".*)


Is there any way to achieve this by replacing only the keywords in alt?

Echo preg_replace ('/alt = "(. + ?) (Combat) (. + ?) "/', 'Alt =" $1 & % $3 "','');

$ Html = 'combat Jianghu '; echo preg_replace ('#(? = [^>] * (? = <(?! /A>) | $) # ',' \ 0', $ html );
Fighting rivers and lakes

Echo preg_replace ('/alt = "(. + ?) (Combat) (. + ?) "/', 'Alt =" $1 & % $3 "','');


Thank you. is PHP regular expression different from others? I used the regular expression tool to match different results from php.

$ Html = 'combat Jianghu '; echo preg_replace ('#(? = [^>] * (? = <(?! /A>) | $) # ',' \ 0', $ html );
Fighting rivers and lakes


Thank you.
Does php regular expressions differ from others? Where is the difference, or the knowledge written in that book?

The manual is in English.
Some online tutorials also mentioned


The contents of a lookbehind assertion are restricted such that all the strings it matches must have a fixed length.
The content limit of a tracing asserted. all strings must have a fixed length.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.