match and replace str attribute in html - php

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 ($).

Related

preg-replace image width, height and style

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" />

How to use regex to replace image names to img tags?

Say I need to replace any of the following:
{{image.jpg}} or {{any-othEr_Fil3.JPG}}
To:
<img src="image.jpg" alt="" /> or <img src="any-othEr_Fil3.JPG" alt="" /> respectively using PHP and regex.
What would be the scheme?
I've been trying, but unsuccessfully.
The regex to match (I assume that file name does not contain } character - if it does contains, then there must be a scheme to escape it, which I don't know from your provided information):
/{{([^}]*)}}/
The string to replace:
'<img src="$1" alt="" />'
Quick fix
To match the characters in between the {{ and the }} we should use (.+?). The . means to match any character including white space. I allowed for this since file name.jpg is a valid file name (if you don't want white space replace .+? with \S+?). The + means that there needs to be more than one character for the match to happen. The ? means that the regexp will try to match as few characters as possible. So then if we use the regexp {{(.+?)}} the captured characters will be those in between the nearest sets of {{ and }}. For example:
$string = '{{image.jpg}} or {{any-othEr_Fil3.JPG}}';
echo preg_replace_callback('/{{(.+?)}}/', function($matches) {
return sprintf('<img src="%s" alt="" />', $matches[1]);
}, $string);
Will echo
<img src="image.jpg" alt="" /> or <img src="any-othEr_Fil3.JPG" alt="" />
Getting fancy
The regexp /{{\s*(.+?\.(?:jpg|png|gif|jpeg))\s*}}/i will match any image file names (with jpg, png, gif, or jpeg file extensions) in between sets of {{ and }} allowing for space in between the curly brackets and the file name. For example :
$string = "{{image.jpg}} or {{ any-othEr_Fil3.JPG }} \n"
. "{{ with_spaces.jpeg }} and {{ this_is_not_an_image_so }} don't replace me \n"
. "{{ demonstrating spaces in file names.png }}";
$regexp = '/{{\s*(.+?\.(?:jpg|png|gif|jpeg))\s*}}/i';
echo preg_replace_callback($regexp, function($matches) {
return sprintf('<img src="%s" alt="" />', $matches[1]);
}, $string);
Will echo
<img src="image.jpg" alt="" /> or <img src="any-othEr_Fil3.JPG" alt="" />
<img src="with_spaces.jpeg" alt="" /> and {{ this_is_not_an_image_so }} don't replace me
<img src="demonstrating spaces in file names.png" alt="" />
More Resources
PHP preg_replace_callback documentation
The site I use to test and practice regular expressions
This is in Perl but should be similar in PHP:
From command line:
echo "{{image.jpg}} {{any-othEr_Fil3.JPG}}" | perl -ne '$_ =~ s/{{([^}]+)}}/<img src="$1" alt="" \/>/g; print $_'

Strip tags, but keep the first one

How can I keep for example the first img tag but strip all the others?
(from a HTML string)
example:
<p>
some text
<img src="aimage.jpg" alt="desc" width="320" height="200" />
<img src="aimagethatneedstoberemoved.jpg" ... />
</p>
so it should be just:
<p>
some text
<img src="aimage.jpg" alt="desc" width="320" height="200" />
</p>
The function from this example can be used to keep the first N IMG tags, and removes all the other <img>s.
// Function to keep first $nrimg IMG tags in $str, and strip all the other <img>s
// From: http://coursesweb.net/php-mysql/
function keepNrImgs($nrimg, $str) {
// gets an array with al <img> tags from $str
if(preg_match_all('/(\<img[^\>]+\>)/i', $str, $mt)) {
// gets array with the <img>s that must be stripped ($nrimg+), and removes them
$remove_img = array_slice($mt[1], $nrimg);
$str = str_ireplace($remove_img, '', $str);
}
return $str;
}
// Test, keeps the first two IMG tags in $str
$str = 'First img: <img src="img1.jpg" alt="img 1" width="30" />, second image: <img src="img_2.jpg" alt="img 2" width="30">, another Img tag <img src="img3.jpg" alt="img 3" width="30" />, etc.';
$str = keepNrImgs(2, $str);
echo $str;
/* Output:
First img: <img src="img1.jpg" alt="img 1" width="30" />, second image: <img src="img_2.jpg" alt="img 2" width="30">, another Img tag , ... etc.
*/
You might be able to accomplish this with a complex regex string, however my suggestion would be to use preg_replace_callback, particularly if you are on php 5.3+ and here's why. http://www.php.net/manual/en/function.preg-replace-callback.php
$tagTracking = array();
preg_replace_callback('/<[^<]+?(>|/>)/', function($match) use($tagTracking) {
// your code to track tags here, and apply as you desire.
});

Regular expression to remove <p> tags from around img elements

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 )

preg replace image link

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.

Categories