I want to parse a html content that have something like this:
<td class="s3_40">12,909</td>
i tried many regex to find string in between (12,909)
like : %<td class=\"s3_40\">(.*)</td>%
but i didn't find that .
Hope this can help:
$str = '<td class="s3_40">12,909</td>';
if (preg_match('#<td class="s3_40">(.*)</td>#s', $str, $matches)) {
var_dump($matches[1]);
}
If you must do it with a regex. You can try a pattern like this:
/<td[^>]*>(.*?)<\/td>/
Try this
#(?<=(s3_40">))([^<]+)(?=(</td>))#
Try doing it like this, just keep adding to s3_41, s3_42, etc to the class you need.
<?php
$s = '<td class="s3_40">12,909</td>';
$pat = '#<td class=".*(s3_40|s3_41).*".*?>(.*?)</td>#isU';
preg_match_all($pat,$s,$ms);
var_dump($ms);
Related
I have a string that looks like this
$t="<b>vist</b>thank you for the follow.";
I am trying to remove the tag b and put an "#" instead of this tag.
I tried this
str_replace("<b></b>","#",$t);
but it doesn't replace the closing tag.
I don't know why it is not working may be there is something omitted in the code.
Try with
$search = array('<b>','</b>');
$replace = '#';
echo str_replace($search, $replace, $t);
To replace multiple words using str_replace() function,
You can Try this
$t="<b>vist</b> thank you for the follow";
$pattern=array();
$pattern[0]="<b>";
$pattern[1]="</b>";
$replacement=array();
$replacement[0]="#";
$replacement[1]="";
echo str_replace($pattern,$replacement,$t);
View the Demo
Try with -
$t="<b>vist</b>";
echo str_replace(array("<b>", "</b>"),"#",$t);
I have a PHP Script that fetches text from the database, and what I want to do is the following:
If the text from the database looks like this:
[url="something"]Some text[/url]
I want it to look like this:
[url=something]Some text[/url]
I hope you can help me.
Thanks
You should use str_replace:
$str = str_replace('"','',$str);
$text = '[url="something"]Some text[/url]';
echo $text = preg_replace('#\[url="(.*?)"\]#i','[url=$1]', $text);
I have some HTML code like this:
<a href="http://mysite.com/documentos/Servicios/SUCRE/sucDoc19.pdf&sa=U&ei=sf0JUrmjIc3Nswb154CgDQ&ved=0CCkQFjAA&usg=AFQjCNGfXg_9x83U3pYr6JfkJcWuXv8X0Q">
I need to clean my code to get something like this
<a href="http://mysite.com/documentos/Servicios/SUCRE/sucDoc19.pdf">
using preg_replace.
My code is the following:
$serp = preg_replace('&sa=(.*)" ', '" ', $serp);
and it doesn't work.
BTW i need to restrict search with preg_replace until the FIRST entrance, i.e. i need to replace all html from &sa= to the FIRST ", but now it search from &sa= to the LAST "...
You're missing the regex delimiters.
$serp = preg_replace('/&sa=(.*)" /', '" ', $serp);
will give you this.
You missed the delimiter.
So your code looks like:
$serp = preg_replace('/&sa=(.*)" /', '" ', $serp);
okay, if you want to delete everything till the first quote then you can try the following instead of regex:
$temp = substr($serp,strpos($serp,'&sa='),strpos($serp,'"',strpos($serp,'&sa=')));
$serp = str_replace($temp,"",$serp);
Just another regex to do it :)
$text = '<a href="http://mysite.com/documentos/Servicios/SUCRE/sucDoc19.pdf&sa=U&ei=sf0JUrmjIc3Nswb154CgDQ&ved=0CCkQFjAA&usg=AFQjCNGfXg_9x83U3pYr6JfkJcWuXv8X0Q" target="_blank">';
$text = preg_replace('/(&sa=[^"]*)/', '', $text);
echo $text;
// Output:
<a href="http://mysite.com/documentos/Servicios/SUCRE/sucDoc19.pdf" target="_blank">
You can try it HERE (thks to hjpotter92 for this tool)
I have a list of words that I'd like to add a link to, I can do this fairly easily using preg_match_all and preg_replace:
$str = "<span class=\"cz\">Dám si jedno pivo prosím.</span> = I'll have a beer please.";
preg_match_all('/[a-zťúůýžáčďéěíňóřš]+/i',$str,$matches);
$matches = array_unique($matches[0]);
foreach ($matches as $match) {
if(!empty($words[$match])) {
$str = preg_replace("/(^|[^\w]){1}(".preg_quote($match,"/").")($|[^\w]){1}/i", '\\1\\2\\3', $str);
}
}
echo $str;
What I'd like to do is restrict the linking to only within the span tag.
My brain is all regex-ed out, so any help would be appreciated! Thanks!
Darren.
preg_match_all('/[a-zťúůýžáčďéěíňóřš]+(?=\s*?</span>)/i',$str,$matches);
/( *<SPAN*>)([^<]*)(<\/SPAN>)/i
I think something like this should work, but will break if you have other tags inside your SPAN. I'd advise you to use the DOM functions instead.
I have a variable $link_item, it's used with echo and gives the strings like
<span class="name">Google</span>http://google.com
How to remove "<span class="name">Google</span>" from string?
It should give just "http://google.com".
Heard it can be done with regex(), please help.
Without regex:
echo substr($link_item, stripos($link_item, 'http:'))
But this only works if the first part (i.e. <span class="name">Google</span>) never contains http:. If you can assure this: here you go :)
Reference: substr, stripos
Update:
As #Gordon points out in his comment, my code is doing the same as strstr() already does. I just put it here in case one does not read the comments:
echo strstr($link_item, 'http://');
$string = '<span class="name">Google</span>http://google.com';
$pieces = explode("</span>",$string);
//In case there is more than one span before the URL
echo $pieces[count($pieces) -1];
Solved:
$contents = '<span class="name">Google</span>http://google.com';
$new_text = preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $contents);
echo $new_text;
// outputs -> http://google.com
Don't use a regex. Use a HTML parser to extract only the text you want from it.
Made myself
$link_item_url = preg_replace('#<span[^>]*?>.*?</span>#si', '', $link_item);
This will remove any <span + something + </span> from variable $link_item.
Thanks for all.