Using regex, how can I get the image from the following?
I am trying preg_match_all("!<img src='(.*?)' alt>!is", $str, $images); without any luck
<div class="film_img">
<img src="image url" alt="text">
</div>
Related
I converted a webiste from Wordpress and I some of the posts have a caption tag as the following:
[caption id="attachment_666" align="alignleft" width="316"]
<img class="wp-image-92692" src="img" width="316" alt="fitbit-yoga-lady.png" height="210">
text
[/caption]
I would like to catch all of these captions and convert it to the following
<div id="attachment_666" style="width: 326px" class="wp-caption alignleft">
<img class="wp-image-92692" src="img" alt="fitbit-yoga-lady.png" width="316" height="210">
<p class="caption">text</p>
</div>
Well, given the exact text that you provided, the following should work.
Search Pattern:
\[caption([^\]]+)align="([^"]+)"\s+width="(\d+)"\](\s*\<img[^>]+>)\s*(.*?)\s*\[\/caption\]
Replacement:
<div\1style="width: \3px" class="wp-caption \2">\4
<p class="caption">\5</p>
</div>
See the demo.
Depending on how tolerant of variations in the input it needs to be, you may need to adjust it from there, but that should at least get you started.
Here's an example of how this could be done with preg_replace:
function convert_caption($content)
{
return preg_replace(
'/\[caption([^\]]+)align="([^"]+)"\s+width="(\d+)"\](\s*\<img[^>]+>)\s*(.*?)\s*\[\/caption\]/i',
'<div\1style="width: \3px" class="wp-caption \2">\4<p class="caption">\5</p></div>',
$content);
}
I'm doing this blindly on my phone, but I think you can use the following two regular expressions, one for the opening tag and another for the closing:
Find:
\[caption([^\]])\]
Replace:
<div$1>
Find:
\[/\caption\]
Replace:
</div>
Not all src attribute in html, Exactly the div ID begins with "post_message_"
HTML CODE
<div id="another_div">
<img src="http://domain.com/folder/somfilename.jpg" border="0" alt="">
</div>
<div id="post_message_412">
<img src="http://domain.com/folder/filename1.jpg" border="0" alt="">
</div>
<div id="post_message_413">
<img src="http://domain.com/folder/filename2.jpg" border="0" alt="">
</div>
<div id="post_message_414">
<img src="http://domain.com/folder/filename3.jpg" border="0" alt="">
</div>
<div id="post_message_415">
<img src="http://domain.com/folder/filename4.jpg" border="0" alt="">
</div>
Get image tag's url with in the div ID begins with "post_message_" (Not like 'another_div' id name) using php preg_match() or preg_match_all() or preg_split() or any other possible solutions.
after that i want to get urls from an array
Assuming the HTML is the same structure as in your example, you can use: <div\s*id="post_message_[^"]+">\s*<img src="([^"]+)
It's only long because of the literals.
\s* matches 0+ whitespace characters
[^"]+ matches characters that aren't "
I need to remove all images in a variable using the following pattern. (With PHP).
<div class="float-right image">
<img class="right" src="http://www.domain.com/media/images/image001.png" alt="">
</div>
All the div tags will have an image class, but the float-right might vary. I can't seem to get the regex working, please help me.
Use a DOM instead of regex. Example:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<div class="float-right image">
<img class="right" src="http://www.domain.com/media/images/image001.png" alt="">
</div>');
foreach( $doc->getElementsByTagName("div") as $old_img ) {
$img = $doc->createElement("img");
$src = $doc->createAttribute('src');
$class = $doc->createAttribute('class');
$src->value = 'http://your.new.link';
$class->value = 'right';
$img->appendChild($src);
$img->appendChild($class);
$doc->replaceChild($img, $old_img);
}
echo $doc->saveHTML();
?>
This regex matches your pattern:
(?s)<div class="[^"]*">\W*<img\W*class="[^"]*"\W*src="[^"]*"\W*alt="[^"]*">\W*</div>
I have tested it against several strings. It will work on:
<div class="anything">
<img class="blah" src="anything" alt="blah">
</div>
, where you can replace the "blah" and "anything" strings with anything.
Also, the various \W* in the regex allow for different spacing from string to string.
You said you want to do this in PHP.
This will zap all the matched patterns from a page stored in the $my_html variable.
$my_html=preg_replace('%(?s)<div class="[^"]*">\W*<img\W*class="[^"]*"\W*src="[^"]*"\W*alt="[^"]*">\W*</div>%m', '', $my_html);
I think this is what you were looking for?
I have the following problem and I am not really good in regular expressions, so please, could somebody help me out?
<div class="bulletPoints">
<div>
<img src="../images/test_f01.jpg" alt="test_f01"/>1
<img src="../images/test_f02.jpg" alt="test_f02"/>2
<img src="../images/test_f03.jpg" alt="test_f03"/>3
<img src="../images/test_f04.jpg" alt="test_f04"/>4
<img src="../images/test_f05.jpg" alt="test_f05"/>5
<img src="../images/test_f06.jpg" alt="test_f06"/>6
<img src="../images/test_f07.jpg" alt="test_f07"/>7
<img src="../images/test_f08.jpg" alt="test_f08"/>8
<img src="../images/test_f09.jpg" alt="test_f09"/>9
<img src="../images/test_f09.jpg" alt="test_f09"/>10
<img src="../images/test_f09.jpg" alt="test_f09"/>11
<!-- and so on -->
</div>
</div>
I need to replace the plain text string after the image tag inside of each anchor tag. The string is always a number from 0 - 99. The IDs of the anchor tags are auto generated and the title attribute, too. I don't know how to reach only the number on all tags. I need to replace it with an empty string ''. Could somebody help me and explain how I can do that??
Thank you very very much!!
Try
preg_replace('/(<img[^>]+>)(\d+)/', "\\1", $text);
This will replace the image tag + text after the image tag with only the image tag.
you can replace />[0-9]{,2}< with /><
test with sed:
echo 'yourtext'|sed -r 's#/>[0-9]{,2}<#/><#g'
output:
<div class="bulletPoints">
<div>
<img src="../images/test_f01.jpg" alt="test_f01"/>
<img src="../images/test_f02.jpg" alt="test_f02"/>
<img src="../images/test_f03.jpg" alt="test_f03"/>
<img src="../images/test_f04.jpg" alt="test_f04"/>
<img src="../images/test_f05.jpg" alt="test_f05"/>
<img src="../images/test_f06.jpg" alt="test_f06"/>
<img src="../images/test_f07.jpg" alt="test_f07"/>
<img src="../images/test_f08.jpg" alt="test_f08"/>
<img src="../images/test_f09.jpg" alt="test_f09"/>
<img src="../images/test_f09.jpg" alt="test_f09"/>
<img src="../images/test_f09.jpg" alt="test_f09"/>
<!-- and so on -->
</div>
</div>
preg_replace('|>[0-9]{1,2}<|','><',$html);
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 ^^