I want to remove specific image from string.
I need to remove Image with specific width and height.
I have tried this, but this will remove first image.
$description = preg_replace('/<img.*?>/', '123', $description, 1);
I want to remove any/all image(s) with specific width and height.
E.g. Remove this image <img width="1" height="1" ..../>
I suggest that you move away from using regex expressions to parse (or manipulate) HTML, because it's not a good idea, and here's a great SO answer on why.
For example, by using Peter's approach (preg_match_all('~<img src="(.+?)" width="(.+?)">~is', $content, $return);), you are assuming that all your images start with <img, are followed by the src, and then contain the width=, all typed exactly like that and with those exact whitespace separations, and those particular quotes. That means that you will not capture any of these perfectly valid HTML images that you want to remove:
<img src='asd' width="123">
<img src="asd" width="123">
<img src="asd" class='abc' width="123">
<img src="asd" width = "123">
While it's of course perfectly possible to catch all these cases, do you really want to go through all that effort? Why reinvent the wheel when you can just parse the HTML with already-existing tools. Take a look at this other question.
Made a little example for you
<?php
$string = 'something something <img src="test.jpg" width="10" height="10" /> and something .. and <img src="test.jpg" width="10" height="10" /> and more and more and more';
preg_match_all('~<img(.+?)width="10"(.+?)height="10"(.+?)/>~is', $string, $return);
foreach ($return[0] as $image) {
$string = str_replace($image, '', $string);
}
echo $string;
I got the solution:
$description = preg_replace('!<img.*?width="1".*?/>!i', '', $description);
Related
I have a blog system where user inputs the image url in the post content like
hey how are you <img src="example.com/image.png" style="width: 952px;">
if the user has written like this
hello how are you <img src="example.com/image.png" style="width: 952px;">
Then I want to find this style width 952px line and replace it into 100%
the user can input any dimension of image like 100px 300px
here is what I have tried:
$content is what user have posted
$go = $content;
$mystr= $go;
$start=strpos($mystr,'style="width: ');
$end=strpos($mystr,'">');
$jo = substr($mystr,0,$start+strlen('') ) . 'style="width: 100%;' .
substr($mystr,$end);
The problem i'm facing is that if user puts two or three image tag but this script is only replacing one width value how to replace multiple width
the user inputs->
<img src="example.com/img.png" style="width: 500px;"><br><img
src="example.com/img2.png" style="width: 952px;">
here what i got as result
<img src="example.com/img.png" style="width: 100%;"><br><img
src="example.com/img2.png" style="width: 952px;">
second image did'nt changed value of width
Use preg_replace function:
$go = preg_replace('/style="width:\s*\d+px;/i', 'style="width:100%;', $content);
That's assuming $content has only your <img> tags, otherwise you may mess up other html elements.
This case is perfect for regular expression search and replace.
In PHP you would use preg_replace , documentation for which you can find here:
- http://php.net/manual/en/function.preg-replace.php
Just use a regex with preg_replace() to replace the width in each img tag, e.g.
echo preg_replace("/<img.*?\Kwidth:\s*[^;]*/s", "width:100%", $input);
Simply exanied we first match everything until the width attribute in a img tag (<img.*?) and then reset the match with \K so we then can match the width attribute(width:\s*[^;]*/) and replace it with width:100%.
$title = $_POST['title'];
$post = stripslashes($_POST['TextArea']);
$link = preg_replace('"(http://www\S+)"','$1', $post);
echo $link;
After submit my form the above script replace all links inside textarea
and the result for images is to be broken.
Is there a way to replace links but not images?
While url works perfect the result for images in browser is
<img src="http://.../myimage.jpg" height="150" width="150">
Thank you
preg_replace('"(?<!src=[\"\'])(http://www\S+)"','$1', $text)
This will only convert http://www links that are not preceded by src=" or src='.
preg_replace('/(?<!src=[\"\'])(http(s)?:\/\/(www\.)?[\/a-zA-Z0-9%\?\.\-]*)(?=$|<|\s)/','$1', $text);
This is the correct way because previous solution does not finish url when necessary
I'm trying to get the first image from each of my posts. This code below works great if I only have one image. But if I have more then one it gives me an image but not always the first.
I really only want the first image. A lot of times the second image is a next button
$texthtml = 'Who is Sara Bareilles on Sing Off<br>
<img alt="Sara" title="Sara" src="475993565.jpg"/><br>
<img alt="Sara" title="Sara two" src="475993434343434.jpg"/><br>';
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $texthtml, $matches);
$first_img = $matches [1] [0];
now I can take this "$first_img" and stick it in front of the short description
<img alt="Sara" title="Sara" src="<?php echo $first_img;?>"/>
If you only need the first source tag, preg_match should do instead of preg_match_all, does this work for you?
<?php
$texthtml = 'Who is Sara Bareilles on Sing Off<br>
<img alt="Sara" title="Sara" src="475993565.jpg"/><br>
<img alt="Sara" title="Sara two" src="475993434343434.jpg"/><br>';
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
echo $image['src'];
?>
Don't use regex to parse html.
Use an html-parsing lib/class, as phpquery:
require 'phpQuery-onefile.php';
$texthtml = 'Who is Sara Bareilles on Sing Off<br>
<img alt="Sarahehe" title="Saraxd" src="475993565.jpg"/><br>
<img alt="Sara" title="Sara two" src="475993434343434.jpg"/><br>';
$pq = phpQuery::newDocumentHTML($texthtml);
$img = $pq->find('img:first');
$src = $img->attr('src');
echo "<img alt='foo' title='baa' src='{$src}'>";
Download: http://code.google.com/p/phpquery/
After testing an answer from here Using regular expressions to extract the first image source from html codes? I got better results with less broken link images than the answer provided here.
While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.
For more consistent results use this object http://simplehtmldom.sourceforge.net/ which allows you to manipulate html.
An example is provided in the response in the first link I posted.
function get_first_image($html){
require_once('SimpleHTML.class.php')
$post_html = str_get_html($html);
$first_img = $post_html->find('img', 0);
if($first_img !== null) {
return $first_img->src';
}
return null;
}
Enjoy
I have some text with images within it. I want to replace specific images within the text with something else.
i.e. the text contains an a youtube img url that I want to replace with the actual video link.
<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">
and replace it with the youtube Iframe code:
<iframe title="'.$id.'" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/'.$id.'" frameborder="0"></iframe>
my function looks like this:
function replacelink($link) {
$find= ("/<img src=[^>]+\>/i");
$replace = youtube("\\2");
return preg_replace($find,$replace);
}
What do I need to change in the regex to do the above?
Your regex is looking for <img src=, but there is a class attribute between img and src. Using $find= '/<img.*src=[^>]+>/i'; corrects the problem; however, this illustrates why you shouldn’t use regex to parse HTML.
You wrote:
I have some text with images within it.
If the text you’re referring to is actually HTML, then there are better alternatives to using regex for this.
Update
I believe this is what you’re looking for.
<?php
function replacelink($text) {
$replace = '<iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" <iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/$2" frameborder="0"></iframe>';
$find = '/(<img.*?alt="([\da-z]+)".*?>)/i';
return preg_replace($find, $replace, $text);
}
$imagestr = '<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">';
echo replacelink($imagestr);
?>
There’s no need for a separate youtube() function.
If you want to replace more than one image, use preg_replace_all() instead of preg_replace().
The following regex would get all the images with a specific url. I not sure if this is what you wanted.
<img [^>]*?src="url"[^>]*?>
Previous anwser would fail if there were more than one image.
I have the following situation, I have an html file with image tags about 10 or more, but it varies. Now what I want to achieve is to replace the image src with a PHP function as example below
I want to replace something like this
<img src="image1.png" ....
<img src="image2.png" ....
with this
<img src="<?=imageResize('image1.png',20,15)?>" ...
<img src="<?=imageResize('image2.png',20,15)?>" ..`.
Is this possible?
Let's assume that the current page source is contained in $source. Try some regular expressions:
<?php
preg_replace ('/<img src="(.+)"/Ui', '<img src="<?=imageResize(\'\\1\',20,15)?>"', $source);
?>
I see 2 cases:
Your imageResize function resizes the image, creating a new one with a different name (something like image120x15.png) and returning a string with the image name. In this case you should do something like:
<img src="<?php echo imageResize('image1.png',20,15);?>" ...
Your imageResize actually resizes the initial image and overriding it. In this case, do something like:
<?php imageResize('image1.png',20,15);?>
<img src="image1.png" ...
Capture output with output buffer and then apply regex for replace.
<?ob_strart();?>
<img src="image1.png" />
<img src="image2.png" />
<?php
$cnt = ob_get_clean();
$cnt = preg_replace ('/<img src="([^"]+)"/', '<img src="<?=imageResize(\'\\1\',20,15)?>"', $cnt);?>
For execute function imageresize you must run output in eval().