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.
Related
I have a question about preg_replace. I have the following HTML in WordPress:
<img width="256" height="256" src="http://localhost/wp-content/uploads/2015/08/spiderman-avatar.png" class="attachment-post-thumbnail wp-post-image" alt="spiderman-avatar">
I change it to the following:
<img src="" data-breakpoint="http://localhost/wp-content/uploads/2015/08/" data-img="theme-{folder}.jpg" class="srcbox" alt="spiderman-avatar">
with the following preg_replace:
$html = preg_replace(
'/src="(https?:\/\/.+\/)(.+\-)([0-9]+)(.jpg|.jpeg|.png|.gif)"/',
'src="" data-breakpoint="$1" data-img="$2{folder}$4"', // Replace and split src attribute into two new attributes
preg_replace(
'/(width|height)="[0-9]*"/',
'', // Remove width and height attributes
preg_replace(
'/<img ?([^>]*)class="([^"]*)"?/',
'<img $1 class="$2 srcbox"', // Add class srcbox to class attribute
$html
)
)
);
I have the feeling I have written some serious slow code, and it can be done in a single preg_replace.
Chris85 mentioned the HTML parser, so I found this and got this so far:
http://nimishprabhu.com/top-10-best-usage-examples-php-simple-html-dom-parser.html
include('simple_html_dom.php');
$html = file_get_html($html);
From here I COULD loop through all images and change the th attribute. But how do I put the new element were it came from?
you should better use DOM
http://php.net/manual/de/domdocument.loadhtml.php
and extract the attributes with it.
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);
I want to get the YouTube video ID from YouTube embed code using preg_match or regex. For a example
<iframe width="560" height="315" src="//www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>
I want to take the ID 0gugBiEkLwU
Can anyone tell me how to do this. Really appropriate your help.
Using this pattern with a capturing group should give you the string you want:
d\/(\w+)\?rel=\d+"
example: https://regex101.com/r/kH5kA7/1
You can use :
src="\/\/(?:https?:\/\/)?.*\/(.*?)\?rel=\d*"
Check Demo Here
Explanation :
I know this is pretty late, but I came up with something for people who might still be looking.
Since not all Youtube iframe src attributes end in "?rel=", and can sometimes end in another query string or end with a double quote, you can use:
/embed\/([\w+\-+]+)[\"\?]/
This captures anything after "/embed/" and before the ending double-quote/query string. The selection can include any letter, number, underscore and hyphen.
Here's a demo with multiple examples: https://regex101.com/r/eW7rC1/1
The below function will extract the youtube video id from teh all format of youtube urls,
function getYoutubeVideoId($iframeCode) {
// Extract video url from embed code
return preg_replace_callback('/<iframe\s+.*?\s+src=(".*?").*?<\/iframe>/', function ($matches) {
// Remove quotes
$youtubeUrl = $matches[1];
$youtubeUrl = trim($youtubeUrl, '"');
$youtubeUrl = trim($youtubeUrl, "'");
// Extract id
preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $youtubeUrl, $videoId);
return $youtubeVideoId = isset($videoId[1]) ? $videoId[1] : "";
}, $iframeCode);
}
$iframeCode = '<iframe width="560" height="315" src="http://www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>';
// Returns youtube video id
echo getYoutubeVideoId($iframeCode);
I'm trying to use preg_replace to filter member comments. To filter script and img tags. If src is from my site, allow it with tags, if from another site, just show the src
Regex Expression:
<(\w+).+src=[\x22|'](?![^\x22']+mysite\.com[^\x22']+)([^\x22']+)[\x22|'].*>(?:</\1>)?
Using:
preg_replace($pattern, $2, $comment);
Comment :
Hi look at this!
<img src="http://www.mysite.com/blah/blah/image.jpg"></img>
<img src="http://mysite.com/blah/blah/image.jpg"></img>
<img src="http://subdomain.mysite.com/blah/blah/image.jpg"/>
<img src="http://www.mysite.fakesite.com/blah/blah/image.jpg"></img>
<img src="http://www.fakesite.com/blah/blah/image.jpg"></img>
<img src="http://fakesite.com/blah/blah/image.jpg"></img>
Which one is your favorite?
Wanted Outcome:
Hi look at this!
<img src="http://www.mysite.com/blah/blah/image.jpg"></img>
<img src="http://mysite.com/blah/blah/image.jpg"></img>
<img src="http://subdomain.mysite.com/blah/blah/image.jpg"/>
http://www.mysite.fakesite.com/blah/blah/image.jpg (notice that it's just url, because it's not from my site)
http://www.fakesite.com/blah/blah/image.jpg
http://fakesite.com/blah/blah/image.jpg
Which one is your favorite?
Anyone see anything wrong?
I'm trying to use preg_replace to filter member comments. To filter script and img tags.
HTML Purifier is going to be the best tool for this purpose, though you want a whitelist of acceptable tags and attributes, not a blacklist of specific harmful tags.
The biggest thing wrong I can see is trying to use regex to modify HTML.
You should use DOMDOcument.
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($content);
foreach($dom->getElementsByTag('img') as $element) {
if ( ! $element->hasAttribute('src')) {
continue;
}
$src = $element->getAttribute('src');
$elementHost = parse_url($src, PHP_URL_HOST);
$thisHost = $_SERVER['SERVER_NAME'];
if ($elementHost != $thisHost) {
$element->parentNode->insertBefore($dom->createTextNode($src), $element);
$element->parentNode->removeChild($element);
}
}
you shoud use im mode;
#<(\w+).+src=[\x22|'](?![^\x22']+mysite\.com[^\x22']+)([^\x22']+)[\x22|'].*>(?:</\1>)?#im
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().