PHP preg_match_all - Get what is inside pattern - php

I have a lot of difficulties in this REGEX stuff, can any one help improve my code?
Thank you in advanced.
What I need is to get the content in side the [Slide] CONTENT [/Slide] tags.
What i am doing is:
preg_match ('/\[Slide\].*\[\/Slide\]/s', $content, $matches);
$conteudo_slide = $matches[0];
$conteudo_full = preg_replace('/\[Slide\]/s', "", $conteudo_slide);
$conteudo_full = preg_replace('/\[\/Slide\]/s', "", $conteudo_full);
the content of the page is:
<p>[Slide]http://www.gprco-cpa.com/images/industries/corporations.jpg[/Slide]</p>
<p>[Slide]http://www.expatcpa.com/Corporation_HQ.jpg[/Slide]</p>
<p><br />[Slide]</p>
<p><a href="http://localhost/~tiago/main_wordpress/?attachment_id=437" rel="attachment wp-att-437">
<img class="alignright size-full wp-image-437" title="lightbulb"src="http://localhost/~tiago/main_wordpress/wpcontent/uploads/2012/09/lightbulb1.jpg" alt="" width="500" height="334" /></a>[/Slide]</p><p> </p>
<p>[Slide]http://www.youtube.com/watch?v=SHVOyGVQ3Tw&feature=g-all-u[/Slide]</p>
IsnĀ“t any more correct way of doing this?
Thank you.

try adding ():
preg_match ('/\[Slide\](.*?)\[\/Slide\]/s', $content, $matches);

Related

PHP RegEx to find image-tags and replace quotes

I need a Php-RegEx to find all image-tags in a php-string and replace all quotes (like ") in image-tag object.
$mystring = '<p class="testclass">test tag with p</p><p><img src="test1.jpg" width="100" /></p><p><img src="test2.jpg" width="100" /></p>';
The result that I need is the following:
<p class="testclass">test tag with p</p><p><img src=:quot:test1.jpg:quot: width=:quot:100:quot: /></p><p><img src=:quot:test2.jpg:quot: width=:quot:100:quot: /></p>
I've tested the following code:
$rep_html = '<p class="testclass">test tag with p</p><p><img src="test1.jpg" width="100" /></p><p><img src="test2.jpg" width="100" /></p>';
$rep_html = preg_replace('#<img ([^"]) />#',":quot:", $rep_html);
or/and:
$rep_html = preg_replace('#<img ([\x22]) />#',":quot:", $rep_html);
... Unfortunately, the code does not work that way. The quotes are not formatted to :quote:.
I do not need Php "domDocument" with "loadHTML" ... Only Php RegEx ...
Maybe someone can help me ?
Search for
<(?!img)[^>]*>[^<]*(*SKIP)(*FAIL)|"
Replace with
:quot:
Regex101 Example with code generator on the left giving you an escaped PHP version of the code.
I've tested the following code:
$rep_html = '<p class="testclass">test tag with p</p><p><img src="test1.jpg" width="100" /></p><p><img src="test2.jpg" width="100" /></p>';
$rep_html = preg_replace('#<img ([^"]) />#',":quot:", $rep_html);
or/and:
$rep_html = preg_replace('#<img ([\x22]) />#',":quot:", $rep_html);
... Unfortunately, the code does not work that way. The quotes are not formatted to :quote:.
With less steps, you can use this pattern:
\G(?:(?!\A)|[^<]*+(?:<(?!img)[^>]*+>[^<]*+)*+<)[^">]*+\K"
demo
or you can use #OnlineCop pattern with the S modifier.

Retrieve data attribute value using preg_match

I have the following HTML:
<div class="video cover" data-thumb="https://i.vimeocdn.com/video/1234567.webp?mw=700&mh=393" style="background-image: url(https://i.vimeocdn.com/video/525930392.webp?mw=700&mh=393);">
I would like to retrieve the data-thumb URL value.
I have attempted to retrieve the value using the following:
$iframe = '<div class="video cover" data-thumb="https://i.vimeocdn.com/video/1234567.webp?mw=700&mh=393" style="background-image: url(https://i.vimeocdn.com/video/525930392.webp?mw=700&mh=393);">';
preg_match('/data-thumb="(.*?)"/', $iframe, $matches);
echo $matches[0];
However, this is not retrieving any matches.
EDIT: Thank you for your help and answers. It appears I made an error with the output of $iframe, which was displaying content from an iframe (doh). So preg_match couldn't target it.
preg_match('/data-thumb="([^"]*)"/', $iframe, $matches);
if (isset($matches[1]))
echo $matches[1]; // echo the value of the data-thumb attribute
This works for me. And indeed, original code works fine too.
Try this regex:
/data-thumb="[\s\S]*?"/

Remove specific image from a string

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

preg_replace of annonying html tags and backslash php

i need help with preg replace and figure it
i have content like
<a href=\http://www.example.com\ target=\_blank\ title=\Cleo's Vip Room\>
<img src=\linkurl.tld\ />
<table style=\font-family:Arial, sans-serif;\ border=\0\ cellspacing=\0\ cellpadding=\0\ align=\center\>
and there are a lot all uses \ instead of "
i want to remove them all and keep only URLs and images to something like [URL]url.tld[/URL] and [img]image source [/img] to use for forums bbcode
help please
Try this:
$html = "<a href=\http://www.example.com\ target=\_blank\ title=\Cleo's Vip Room\>
<img src=\linkurl.tld\ />
<table style=\font-family:Arial, sans-serif;\ border=\0\ cellspacing=\0\ cellpadding=\0\ align=\center\>";
$newHtml = preg_replace('/\\\\/', '"', $html);
echo $newHtml;

Replace selfmade image-dl-attribute into correct image dl tag

i want to replace all my images (with an own special attribute)
<img width="720" height="321" dl="text-text-text-text" alt="Marl's Palace of Sin" src="/images/sin-ambiente.jpg" title="Innenansichten des Hauses">
into this
<dl class="imgLeft">
<dt>
<img width="720" height="321" alt="Marl's Palace of Sin" src="/images/sin-ambiente.jpg" title="Innenansichten des Hauses">
</dt>
<dd>text-text-text-text</dd>
</dl>
my php function
function dlSytler($htmlContent)
{
$regex = '#<img([^>]*).*dl="([^"/]*/?[^".]*\.[^"]*)"([^>]*).*>#';
$replace = '<dl class="imgLeft"><dt><img$1 $3> </dt> <dd>$2</dd> </dl>';
$htmlContent = preg_replace($regex, $replace, $htmlContent);
return $htmlContent;
}
the problem is, that the replace doesnt work an every image (and i dont know why).
I think my regex-rule is not perfect. Can anybody help me?
You ask where the problem is and I'll try to give you some pointers.
For starters, the idea of [^>]*.* makes no sense. It's saying something like "match X specifically or nothing, or match anything or nothing." In that case, it would be easy to just match anything.
This is simpler.
Search: (<img.*?)(dl=.*?)(alt=[^>]*>)
Replace: <dl class="imgLeft"><dt>\1\3</dt><dd>\2</dd></dl>
Demo

Categories