Before giving negative response please read.
It's given
[et_pb_image admin_label="Image" src="https://scontent-mia1-1.xx.fbcdn.net/hphotos-xaf1/v/t1.0-9/10848034_8974277986_916022442694_n.jpg?oh=9edafcdfb85e2c1b30ed77e6fb8&oe=566091B0" show_in_lightbox="off" url="" url_new_window="off" animation="left" sticky="off" align="left" force_fullwidth="off" always_center_on_mobile="on" use_border_color="off" border_color="#ffffff" border_style="solid" /]
How can i take the first src url?
this is not working for me
$new = preg_match('/src="(.*)|[^"]"/iS', $y->post_content, $img);
echo '<pre>'; print_r($new); echo '</pre>';
The following should work:
$myString = '[et_pb_image admin_label="Image" src="https://scontent-mia1-1.xx.fbcdn.net/hphotos-xaf1/v/t1.0-9/10848034_8974277986_916022442694_n.jpg?oh=9edafcdfb85e2c1b30ed77e6fb8&oe=566091B0" show_in_lightbox="off" url="" url_new_window="off" animation="left" sticky="off" align="left" force_fullwidth="off" always_center_on_mobile="on" use_border_color="off" border_color="#ffffff" border_style="solid" /]';
if(preg_match('/src="([^"]*)/i', $myString , $img)) {
echo '<pre>';
print_r($img);
$src = $img[1];
echo '</pre>';
echo '<hr />' . $src;
}
What's wrong with your code:
preg_match returns a boolean. True when something is found.
src="(.*)|[^"]" is wrong. src=" is correct. After that you're going to match everything until the end of the line. That's not what you want.
Furthermore $img should contain your result.
Why the above is working:
/src=" # match the src
(
[^"] # matches everything except "
*) # capture the content between the quotes "[the src]"
/ix
You can find a working version here: http://sandbox.onlinephpfunctions.com/code/c71983343308a29272d90a441486474148ab3995
Related
I use this code:
<?php
$texthtml = '<p>test</p><br><p><img src="1.jpeg" alt=""><br></p><p><img src="2.png" alt=""><br><img src="3.png" alt=""></p>';
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
echo $image['src'];
?>
However, when I test it, I get last image (3.png) from a string.
I want to know how can I do for get first image (1.jpeg) in a string.
Try:
preg_match('/<img(?: [^<>]*?)?src=([\'"])(.*?)\1/', $texthtml, $image);
echo isset($image[1]) ? $image[1] : 'default.png';
Regex is not suitable for html tags.
You can read about it here: RegEx match open tags except XHTML self-contained tags
I suggest DOM document if it's more complex than what you have shown here.
If it's not more complex than this I suggest strpos to find the words and "trim" it with substr.
$texthtml = '<p>test</p><br><p><img src="1.jpeg" alt=""><br></p><p><img src="2.png" alt=""><br><img src="3.png" alt=""></p>';
$search = 'img src="';
$pos = strpos($texthtml, $search)+ strlen($search); // find postition of img src" and add lenght of img src"
$lenght= strpos($texthtml, '"', $pos)-$pos; // find ending " and subtract $pos to find image lenght.
echo substr($texthtml, $pos, $lenght); // 1.jpeg
https://3v4l.org/48iiI
In this code, when I use ":-)" emoji doesn't show in output.
But when use "1f60a" OR "1f60c" OR "e252" emoji are shown. What's the problem?
<?php
$emoji_url = "http://coremobile.ir/images_smileys";
$emoji_style = "";
$emoji_code = array(
":-)",
"1f60a",
"1f60c",
"e252"
);
$emoji_img = array(
'<img src="'.$emoji_url.'/1f60a.png" '.$emoji_style.'>',
'<img src="'.$emoji_url.'/1f60a.png" '.$emoji_style.'>',
'<img src="'.$emoji_url.'/1f60c.png" '.$emoji_style.'>',
'<img src="'.$emoji_url.'/e252.png" '.$emoji_style.'>'
);
$ret = 'This Test :-) 1f60a';
$ret = str_replace($emoji_code, $emoji_img, $ret);
echo $ret;
?>
This should work for you:
(Just use strtr() instead of str_replace(), so that it won't go through the string multiple times)
$ret = strtr($ret, array_combine($emoji_code, $emoji_img));
output:
This Test
The otherone didn't worked, because it replaced every match for the first replacement and then the second and so on.
0 replaced:
This Test :-) 1f60a
//^^^ match
first replaced:
This Test <img src="http://coremobile.ir/images_smileys/1f60a.png" > 1f60a
//^^^^^ match ^^^^^ match
second replaced:
This Test <img src="http://coremobile.ir/images_smileys/<img src="http://coremobile.ir/images_smileys/1f60a.png" >.png" > <img src="http://coremobile.ir/images_smileys/1f60a.png" >
$str = '<div class="rss"><img src="http://www.wired.com/images_blogs/gadgetlab/2013/10/1125_hbogo_660-660x436.jpg" alt="You Can Now Get HBO GO Without Paying for Other Channels">
</div>Fans of';
I'm trying to get hold of the text after the <div class="rss"></div> but each expression I use doesn't seem to work.
matching .rss
if(preg_match('/^(<div class=\"rss\">[\S\s]+?</div>([\S\s]*)$/i', $item_content, $matches)) {
Could someone please help with this expression?
Originally I had this expression to match an image tag instead of a div and this worked fine by using
if(preg_match('/^(<img[\S\s]+?>)([\S\s]*)$/i', $item_content, $matches)) {
I didn't go deeply for the regex but yours work well with just solving some syntax problems.
It should be:
^<div class=\"rss\">[\S\s]+?<\/div>([\S\s]*)$/i
Live demo
This may help:
<?php
$item_content = '<div class="rss"><img src="http://www.wired.com/images_blogs/gadgetlab/2013/10/1125_hbogo_660-660x436.jpg" alt="You Can Now Get HBO GO Without Paying for Other Channels">
</div>Fans of';
if(preg_match('/^(<div class=\"rss\">[\S\s]+?<\/div>)([\S\s]*)$/i', $item_content, $matches)) {
$div = $matches[1];
$text = $matches[2];
echo "<textarea style=\"width: 600px; height: 300px;\">";
echo $div . "\n";
echo $text . "\n";
echo "</textarea>";
}
?>
foreach($ret as $object)
{
$res = $object->...;
$img_src = $res[0]->src;
echo $img_src . '<br />';
echo str_replace("&size=2", "", $img_src) . '<br /><br />';
}
$img_src ~ 'http://site.com/img.jpg&size=2'
And I have to receive same link but without &size=2. Why doesn't work my last line in code. It shows the same url.
Are you absolutely certain there are any goofy unprintable characters in your source string? Try debugging with this:
printf("%s\n", join(':', str_split($img_src)));
And make sure you really have &size=2 in your string. If you see two consecutive colons, you've got something like a \0 or some other character mucking up the works in the middle of your string.
Seems to work on this end:
http://site.com/img.jpg&size=2
http://site.com/img.jpg
from
<?php
$img_src = 'http://site.com/img.jpg&size=2';
echo $img_src.'<br />';
echo str_replace("&size=2", "", $img_src).'<br/><br/>';
?>
use preg_replace:
$c=preg_replace("/&size=2/","",$img_src);
Example of usage
<?php
$sr="http://site.com/img.jpg&size=2";
echo preg_replace("/&size=2/","",$sr);
?>
This will output
http://site.com/img.jpg
In the following code, I need to remove <div class="grid_8"></div>.
They will always be at the start and finish of my string, for example:
<div class="grid_8"><img src="http://rps.sanscode.com/site/assets/media/images/rps_mini_logo.png" border="0" alt="Rapid Print Solutions" style="margin-bottom: 30px;" />
<h1></h1>
</div>
What is a suitable regex for preg_replace to remove it? 8 can be any number between 1 and 16.
Thanks
Jason
#Amjad...
Here is my code
public function fix_grid(){
$result = db::query("select * from sc_content_components where component_value_1 like '%grid_%'")->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $item){
$json = json_decode($item['component_value_1']);
if(is_null($json)) continue;
$x = reset($json);
echo htmlspecialchars($x);
echo "<p>=======================<b>Changes to: </b></p>";
$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
, '/<\/(\div+)[^>]*>$/');
$x = preg_replace($patterns, array('',''), trim($x));
echo htmlspecialchars($x);
echo "<hr>";
$json[0]=$x;
// $ne['component_value_1'] = json_encode($json);
// db::where('component_id', $item['component_id']);
// db::update('sc_content_component', $ne);
}
}
I'm using the regex below (#Amjad Masad) and it doesn't remove the last div.
As you can see I am using trim and it doesn't seem to work
For the stated problem, this is the solution:
Edit: expanded regex =
/^\s*<div\s (?:".*?"|\'.*?\'|[^>]*?)*
(?<=\s)class\s*=\s*(["\'])\s*grid_(?:1[0-6]|[1-9])\s*\1
(?:".*?"|\'.*?\'|[^>]*?)*
>
(.*)
<\/div\s*>
\s*$/xs
replacement = "$2"
For the open
$patterns = array('/^<(div)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:\'[^\']*\')|[^>\s]+))?)*)\s*(\/?)>/'
, '/<\/div[^>]*>$/');
preg_replace($patterns, array('','');, trim($htmlString));
Somewhat simpler.
If the 1 to 16 is significant and part of a greater range...
$match = preg_replace("/^<([^>]*)grid_(1[0-6]|[1-9])([^>\d]*)>(.+)<([^>]*)>$/s","$4",trim($str),-1,$hmany);
if($hmany){
echo "$match <br>";
}else{ echo "No match found! <br>"; }
If the 1 to 16 is the only possible range and therefore irrelevant...
$match = preg_replace("/^<([^>]*)>(.+)<([^>]*)>$/s","$2",trim($str),-1,$hmany);
if($hmany){
echo "$match <br>";
}else{ echo "No match found! <br>"; }
Regards.