Reputation: 267
I want to remove alt attribute using preg_replace. Here is the pattern of alt attribute
alt="Screen Shot 2015-06-09 at 11.37.40 AM"
or it might be like this
alt="The Postmates sign outside the office"
There can be any character inside the quotes. I was using following expression:
$html = preg_replace('/alt="\w+\s\w+"/', '><br>', $html);
but it replaces only alt attributes of type:
alt="The Postmates"
What would be the appropriate expression here?
Upvotes: 0
Views: 1177
Reputation: 23892
The .*?
should do it.
$html = preg_replace('/alt=".*?"/', '<br>', $html);
This will replace everything in alt
attribute and its contents with a <br>
.
Sidenote a <br>
in an img
element doesn't make much sense if that's what you are doing... or since you had ><br>
maybe you're trying to close the img
? Is alt
always the last attribute?
The .*?
says find anything until the first "this character", in this case a double quote.... In More detail...
.
= any character
*
= 0 or more occurrences of the preceding character (replace with +
if you want atleast one occurrence)
?
= makes the *
lazy, meaning stop at the first occurrence, not the last
A place to test regexs. http://regex101.com, http://www.phpliveregex.com/
A place to learn regexs, http://www.rexegg.com/, http://www.regular-expressions.info/, and many other sites.
Here's a test you can see this regex work with http://www.phpliveregex.com/p/bvn.
Per your update:
$html = preg_replace('/alt=".*?"\s*/\?s*>/', '><br>', $html);
The /\
is to escape the forward slash because forward slashes are your delimiter. The ?
after the forward slash is because it is optional. The \s*
is zero or more whitespace. Those could be on either side of the optional closing forward slash which is why we have both.
Upvotes: 2
Reputation: 34189
I am not sure how regexp in PHP differs, but try this one:
$html = preg_replace('~alt\s*=\s*"([^"]*)"~', '><br>', $html);
In the beginning we have alt which is a plain text.
Then \s* means "zero or more spaces". It is done to work on code like alt = "something"
.
= stands for a plain equals sign.
\s* again.
([^"]*) means "any symbol except for quotes.
Upvotes: 0