preg replace image link - php

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.

Related

get image src from HTML with regex

I have HTML like
<td class="td_scheda_modello_dati">
<img src="/webapp/safilo/gen_img/p_verde.gif" width="15" height="15" alt="" border="0">
</td>
I want to extract the img src from this HTML using preg_match_all().
I have done this
preg_match_all('#<td class=td_scheda_modello_dati>(.*)<td>#',$detail,$detailsav);
It should give the whole img tag.But it doesn't give me the img tag. So what changes should be done to get the specific value?
Long story short: ideone
You should not use Regex, but instead an HTML parser. Here's how.
<?php
$html = '<img src="/webapp/safilo/gen_img/p_verde.gif" width="15" height="15" alt="" border="0">';
$xpath = new DOMXPath(#DOMDocument::loadHTML($html));
$src = $xpath->evaluate("string(//img/#src)");
echo $src;
?>
Try this code.
$html_text = '<td class="td_scheda_modello_dati">
<img src="/webapp/safilo/gen_img/p_verde.gif" width="15" height="15" alt="" border="0"></td>';
preg_match( '/src="([^"]*)"/i', $html_text , $res_array ) ;
print_r($res_array);
Try using the s modifier after your regex. The default behavior for the dot character is not to match newlines (which your example has).
Something like:
preg_match_all('#<td class=td_scheda_modello_dati>(.*)</td>#s',$detail,$detailsav);
Should do the trick.
It's worth reading up a bit on modifiers, the more you do with regex the more useful they become.
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
Edit: also, just realized that the code posted was missing a closing td tag (it was <td> instead of </td>). Fixed my example to reflect that.
Try this: <img[^>]*src="([^"]*/gen_img/p_verde.gif)"

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 $_'

Preg_replace cant replace what i need

I need some help with preg_replace. At my website I need to delete some things I don't need.
Here is an example:
[caption id="attachment_100951" align="alignleft" width="448" caption="THIS IS WHAT I NEED"] [/caption]
Ok, all I need from this string is: the text inside caption="THIS TEXT",
every thing else I need to be deleted, I have used Google and tried some examples but nothing.
Maybe I need to use another function, but from what I have read on the internet this should replace .
Please help me, it's very important.
Thank you.
EDIT:
The code has some other things that i have forgot.
[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
[/caption]
So i need to get the CAPTION text and delete every thing in
[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
but not
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
also want delete [/caption]
too.
This regex :
$result = preg_replace('/\[caption\s+.*?caption\s*=(["\'])(.*?)\1.*?\[\/caption\]/', '$2', $subject);
will output :
THIS IS WHAT I NEED
when applied to :
[caption id="attachment_100951" align="alignleft" width="448" caption="THIS IS WHAT I NEED"] [/caption]
Updated answer based on your updated question :
$result = preg_replace('%\[caption\s+.*?caption\s*=(["\'])(.*?)\1\s*\](.*?)\[/caption\]%s', '$2\n$3', $subject);
The above regex applied to :
[caption id="attachment_100951" align="alignleft" width="448" caption="eaaaaaaaaaaaaaaaaaa"]
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
[/caption]
Will output :
eaaaaaaaaaaaaaaaaaa
<a href="http://localhost/111baneease1.jpg">
<img class="size-full wp-image-100951" title="zjarr_banese1" src="http://localhost/111baneease1.jpg" alt="eeeeeeeeeeeeeeeee" width="448" height="308" />
</a>
I am not sure if this is exactly what you wanted. Of course you can use the regex to match and do whatever you want with groups $2 and $3...

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.
});

php tags with regex preg_replace

I have a url let's say : https://stackoverflow.com/example1/
and images tags
<img f="/example2"></img>
<img f="example3"></img>
<img f="http://example4.com"></img>
<img f="https://example5.com"></img>
and here is my regex code for preg_replace:
$regex = "-(src\s*=\s*['\"])(((?!'|\"|http://|https://).)*)(['\"])-i";
so if I use this code in the tags above the output will be :
<img f="https://stackoverflow.com/example1//example2></img>
<img f="https://stackoverflow.com/example1/example3></img>
<img f="http://example4.com/"></img>
<img f="https://example5.com/"></img>
but what I want is /example2 and example3 get replaced too but for /example2 it gets only https://stackoverflow.com/ and for example3 it gets https://stackoverflow.com/example1
ps : I know how to get domain using parse_url. and f= is src ^^

Categories