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" />
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 ($).
I am shifting an image tag from one place to another. It currently takes 2 regular expressions, one to find the image tag and one to replace it. Can this be done with one regular expression?
<?php
// Always shorttag
$thumbnail_match_result = preg_match('{<\s*img.*?/>}', $thumbnail, $matches);
$thumbnail_tag = array_shift($matches);
$thumbnail_caption = preg_replace('{<\s*img.*?/>}',"", $thumbnail);
?>
<h4><?php print $title ?></h4>
<a title="<?php print $title ?>" href="<?php print $original_image ?>" data-gallery="">
<?php print $thumbnail_tag ?>
</a>
<?php print $thumbnail_caption; ?>
Thumbnail looks like:
<img typeof="foaf:Image" class="img-responsive" src="/files/styles/photocomp_large/public/lovejoymask400mm_0.jpg?itok=pqICHn8s" width="960" height="656" alt="aly" title="title" /> <blockquote class="image-field-caption">test</blockquote>
Use preg_replace_callback:
$thumbnail_tag='';
$thumbnail_caption = preg_replace_callback('{<\s*img.*?/>}', function($m)
use(&$thumbnail_tag) { $thumbnail_tag=$m[0]; return '';}, $thumbnail);
Check values:
echo $thumbnail_tag . "\n";
//=> <img typeof="foaf:Image" class="img-responsive" src="/files/styles/photocomp_large/public/lovejoymask400mm_0.jpg?itok=pqICHn8s" width="960" height="656" alt="aly" title="title" />
echo $thumbnail_caption . "\n";
//=> <blockquote class="image-field-caption">test</blockquote>
You could use two capture groups, one for each part (image tag, text), like this:
preg_match('{(<\s*img.*?/>)(.*)}', $thumbnail, $matches);
Then $matches will contain the complete matched string in index 0, which you don't need, but in index 1 and 2 you will find the captured groups:
print $matches[1]; // <img ... />
print $matches[2]; // <blocknote>text...
I'm a bit lost here. I'm trying to replace src tag with data:image/gif;base64,R0lGODlhAQABAPABAAAAAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw== for lazy loading and add class lazyload so lazysizes could work. But none of that is successful here.
Am I missing something?
public static function filter_lazy_load_gravatar( $html ) {
$html = preg_replace( '/src="(http:\/\/([^\/]+)\/)?([^"]+)"/', 'src="data:image/gif;base64,R0lGODlhAQABAPABAAAAAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="', $html);
$html = str_replace( 'srcset=', 'data-srcset=', $html );
$pat = '/class="([^"]*)"/';
$html = preg_replace($pat, 'class="$1 lazyload"', $html);
return $html;
}
Input
<img alt="" src="http://gravatar.com/image.jpg" srcset="http://0.gravatar.com/avatar/0b11f7eec98d2ee78414a4322ea94a01?s=400&d=mm&r=g 2x" class="avatar avatar-200 photo" height="200" width="200">
Expected output
<img alt="" src="data:image/gif;base64,R0lGODlhAQABAPABAAAAAP///yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" data-srcset="http://0.gravatar.com/avatar/0b11f7eec98d2ee78414a4322ea94a01?s=400&d=mm&r=g 2x" class="avatar avatar-200 photo lazyload" height="200" width="200">
i want get first img from html code by php
i have this code
$text = '
<b>hello</b>
this is the first img
<img src="http://localhost/1.png" title="first img" />
other img
<img src="http://localhost/2.png" title="other" />
';
I want the first source image in a new variable
http://localhost/1.png
thanks
Use preg_match function
preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $text, $img);
echo $img[1]; // http://localhost/1.png
$text = '
<b>hello</b>
this is the first img
<img src="http://localhost/1.png" title="first img" />
other img
<img src="http://localhost/2.png" title="other" />
';
$pattern = '/src=\"(.*?)\"/g';
preg_match($pattern, $text, $matches);
print_r($matches);
$text = '
<b>hello</b>
this is the first img
<img src="http://localhost/1.png" title="first img" />
other img
<img src="http://localhost/2.png" title="other" />
';
preg_match('/<img.+src=[\'"](?P<src>.+)[\'"].*>/i', $text , $image);
echo $image['src'];
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'