i want a regex pattern to remove images which src attribute is empty, for example :
$html = '<img src="adasas.jpg" /><br />asasas<br />sdfsdf<br /><img title="asa" src="" />';
or
$html = '<img src="adasas.jpg" /><br />asasas<br />sdfsdf<br /><img title="asa" src="" />';
if this <img exist between <a> tag, i want also remove all ( <a and <img ) .
I Tested below code, but it removed all of $html
echo preg_replace( '!(<a([^>]+)>)?<img(.*?)src=""([^>]+)>(</a>)?!si' , '' , $html );
Can anybody help to me ?
thanks in advance
Your problem is likely that the generic .*? matched too much. Rather use [^>]* like in the other parts of the pattern:
'!(<a\s[^>]+>)?<img([^>]+)src=""([^>]*)>(</a>)?!i'
Related
I have a string that contains a few substrings that look like src="somethin/somethin/the_thing.ext" and I would like to transform each of them into src="the_thing.ext"
src="/1/2/3.ext" -> src="3.ext"
src="a/b/c.ext" -> src="c.ext"
src="../d/e.ext" -> src="e.ext"
src="f/g.ext" -> g.ext
src="h.ext" -> h.ext
I'm trying to do it in PHP with preg_replace or ereg_replace
but can figure out how to group the subexpressions.
My expression consist of zero or more something/ and at the end there is some text (the first something can be preceded with just a /)
<?php
$string = '<img src="x.y" alt="" /> <img src="uploads/RTE.jpg"
alt="" /><br /><img src="../uploads/RTEjpg" alt="" /> <img
src="/fileadmin/CPE.ztc">'
$pattern = '/src=\"(.*?)\"/';
$replacement = 'src="';
echo ereg_replace($pattern, $replacement, $string);
?>
I'm stuck with the pattern and replacemnt, how to write them in regex or extended regex?
Use lookbehind and lookahead and then preg_replace:
$string = '<img src="x.y" alt="" /> <img src="uploads/RTE.jpg"
alt="" /><br /><img src="../uploads/RTE.jpg" alt="" /> <img
src="/fileadmin/CPE.ztc">';
$pattern = '/(?<=src=")[^"]+(?<=\/)/';
$replacement = '';
echo preg_replace($pattern, $replacement, $string);
Output:
<img src="x.y" alt="" /> <img src="RTE.jpg" alt="" /><br /><img src="RTE.jpg" alt="" /> <img src="CPE.ztc">
You will need to do two things:
You will need to use a proper framework to get the content of the src tag you are after. Regular expressions on their own is not the recommended way. This previous SO question should point you in the right direction.
Use an expression such as this: (\w+\.\w+?$) (example here) and replace the content of the src tag with the content of the 1st regex group. This expression will match one or more letters, numbers or underscores (\w+) followed by the period character (.), followed by more letters, numbers or underscores and finally the end of the string ($).
My image looks like this:
<img alt="" width="146" height="109" src="http://url.to/src.jpg" style="float:left" />
but i can't figure out how to bring it with preg_replace or preg_replace_callback to this:
<img alt="" src="http://url.to/src.jpg" style="width:146;height:109;float:left">
This works with height and width but I can't get the style-element "float:left" added
$html='<img alt="" width="146" height="109" src="http://url.to/src.jpg" style="float:left" />';
$pattern = ('/<img[^>]*width="(\d+)"\s+height="(\d+)">/');
preg_match($pattern, $html, $matches);
$style = "<img style=\"width:".$matches[1]."px; height:".$matches[2]."px;\"";
$html = preg_replace($pattern, $style, $html);
result of this will be
<img alt="" style="width:146;height:109" src="http://url.to/src.jpg" style="float:left">
which didn't work because of the double style element
Try the following regular expression
<?php
$html='<img alt="" width="146" height="109" src="http://placehold.it/140x200" style="float:left" />';
$pattern = '/(<img.*)width="(\d+)" height="(\d+)"(.*style=")(.*)" \/(>)/';
$style = '$1$4width:$2px;height:$3px;$5';
$html = preg_replace($pattern, $style, $html);
echo $html; //view source of page to see the code change
?>
Note the use of brackets '(' ')' to create groups matched that can be later referenced using $1 $2 etc go to regex101.com and try out the regular expression.
Above code will result in following, except the last part, that shouldn't matter but you can modify it further.
<img alt="" src="http://placehold.it/140x200" style="width:146;height:109;float:left" />
I'm trying to make this:
<span class="introduction">
<img alt="image" src="/picture.jpg" />
</span>
transform into this:
<img alt="image" src="/picture.jpg" />
How would I do this with regex? That is, how do I extract ONLY the img-tag from a given string of html?
Note: There can be a lot more html within the introduction-tag BUT only one img-tag
You shouldn't really use regex on HTML, what about this:?
$string = '<span class="introduction"><img alt="image" src="/picture.jpg" /></span>';
echo strip_tags($string, '<img>');
Otherwise I would use an HTML/XML parser
how about
"<img[^>]*>"
try with grep
kent$ echo '<span class="introduction">
quote> <img alt="image" src="/picture.jpg" />
quote> </span>
quote> '|grep -P "<img[^>]*>"
<img alt="image" src="/picture.jpg" />
preg_match('#(<img.*?>)#', $string, $results);
should work, result in $results[1]
Use DOM and this XPath:
//span[#class="introduction"]/img
to find all img elements that are direct children of any span element with a class attribute of introduction.
I've come to this solution
/<img ([^>"']*("[^"]*"|'[^']*')?[^>"']*)*>/
tested on
<other html elements like span or whatever><img src="asd>qwe" attr1='asd>qwe' attr2='as"dqwe' attr3="as'dqwe" ></other html elements like span or whatever>
I am looking for a regex to convert all
<p><img /></p>
to simply
<img />
The img tags will be fully populated such as
<img src="/file.jpg" width="1" height="2" />
Thank you for your input!
This will work if there is nothing else on the line except the three tags. Let me know if you want it explained further.
$str = "<p><img src=\"/file.jpg\" width=\"1\" height=\"2\" /></p>"
$replaced = preg_replace ( "/<p[^>]*?>(<img[^>]+>)<\/p>/" , "$1" , $str )
I'm trying to preg_replace image link, but only if it ends with .jpg, .gif, .png.
http://exampel.com/1/themes/b2/images/4/image1.jpg
to
<img src="http://exampel.com/1/themes/b2/images/4/image1.jpg" alt="" width="" height="" />
Can someone help me?
Not sure it'll fully answer your question, but you should at least be able to use this as a basis...
What about something like this :
$str = <<<TEST
here is
some text and an image http://exampel.com/1/themes/b2/images/4/image1.jpg ?
and some other http://exampel.com/1/themes/b2/images/4/image1.gif and
a link that should not be replaced : http://exampel.com/test.html !
TEST;
$output = preg_replace('#(http://([^\s]*)\.(jpg|gif|png))#',
'<img src="$1" alt="" width="" height="" />', $str);
var_dump($output);
Which gets me, for my example, the following output :
string 'here is
some text and an image <img src="http://exampel.com/1/themes/b2/images/4/image1.jpg" alt="" width="" height="" /> ?
and some other <img src="http://exampel.com/1/themes/b2/images/4/image1.gif" alt="" width="" height="" /> and
a link that should not be replaced : http://exampel.com/test.html !' (length=301)
The two images links have been replaced, and nothing else has been touched -- in particular, not the non-image link.
The regex I used matches :
something that starts with http://
contains anything that's not a white-character (space, tabulation, newline) : [^\s]*
then, contains a dot : \.
and, finally, one of the extensions you defined as corresponding to an image : (jpg|gif|png)
Then, all that matched string is injected into an <img> tag.