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...
Related
Hello I have this kind of string:
<a href="https://example.com/wp-content/uploads/2016/08/color-2.jpg"><img class="wp-image-5814 size-full aligncenter" src="hhttps://example.com/wp-content/uploads/2016/08/color-2.jpg" class="class">..
I want to find all phrases like it: https://example.com/wp-content/uploads/2016/08/color-2.jpg using this kind of regular expression:
preg_match_all('("https:\/\/example.com\/wp-content\/uploads.+jpg)',
But it returns:
https://ubetonowani.pl/wp-content/uploads/2016/08/kolory-2.jpg"><img class=" wp-image-5814 size-full aligncenter" src="https://ubetonowani.pl/wp-content/uploads/2016/08/kolory-2.jpg class=""
How to do that it returns on first .jpg not last?
Thanks in adnvance
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>
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'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.
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 ^^