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.
Related
I wrote a code which adds hyperlink to all plain text where it finds http:// or https://. The code works pretty well for https://www.google.com and http://yahoo.com. It converts these text into clickable hyperlink with correct address.
<?php
function convert_text_to_link($str)
{
$pattern = "/(?:(https?):\/\/([^\s<]+)|(www\.[^\s<]+?\.[^\s<]+))(?<![\.,:])/i";
return preg_replace($pattern, "<a href='$0' target='_blank'>$0</a>", $str);
}
$str = "https://www.google.com is the biggest search engine. It's competitors are http://yahoo.com and www.bing.com.";
echo convert_text_to_link($str);
?>
But when my code sees www.bing.com, though it adds hyperlink to it but the href attribute also becomes www.bing.com. There is no http:// prepended it. Therefore the link becomes unusable without the link http://localhost/myproject/www.bing.com will go nowhere.
How can I add http:// to www.bing.com so that it should become http://www.bing.com?
Here is your function. Try this.
function convert_text_to_link($str) {
$pattern = '#(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])#';
return preg_replace($pattern, '$0', $str);
}
You should try and check if this works:
window.location = window.location.href.replace(/^www./, 'https:');
might be you will get your solution.
I just got to know about some other approaches too, you can try them out as per your code and requirements:
1.
str_replace("www.","http://","$str");
The test here is case-sensitive. This means that if the string is initially this will change it to http://Http://example.com which is probably not what you want.
try regex:
if (!$str.match(/^[a-zA-Z]+:\/\//))
{
$str = 'http://' + $str;
}.
hope this helps.
Hello Stackers,
I'm just having a small PHP Question about the str_replace() Function. When I replace something, it will just replace everything; That's okay. But what I would like to know is this:
str_replace("*", "<strong>", $message);
Is it possible to use str_replace for codes like * This content is Bold *, just having the content, but still replacing the asterisk's with <strong> and </strong>?
Example:
Original: **This Should be Bold**
After Replacing: <strong>This Should be Bold</strong>
For people flagging this as a Duplicate: It's not about closing HTML Tags, it's about replacing.
I Hope I'm not that unclear. Thanks.
Use regular expression instead; it's more convenient:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong>$1</strong>', $message);
echo $message;
Or if you want to limit the number of asteroids to 2:
'#\*{1,2}([^\*]+)\*{1,2}#m'
You can also do like this
https://eval.in/518881
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong>$1</strong>", $string);
echo $string;
?>
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 the following string ($line) in a PHP function.
$line='<span class="cooktime">25 Mins<span class="value-title" title="PT0H25M"></span></span><span class="preptime">30 Mins<span class="value-title" title="PT0H30M"></span></span>';
I would like to transform each span set to the following using preg_replace or any other approach:
$line='<span class="cooktime">25 Mins<span class="value-title" itemprop="cookTime" content="PT0H25M"></span></span><span class="preptime">30 Mins<span class="value-title" itemprop="prepTime" title="PT0H30M"></span></span>';
Essentially I'm adding itemprop property to span block and renaming title attribute to content.
How do I achieve this?
Thanks for your time, JK
Could try something like this? (not tested in PHP, but tested the RegEx using JavaScript and it seemed to work.)
preg_replace("^(.+)title(.+)$","$1itemprop=\"prepTime\" content$2",$line);
Have a try with:
$str = '<span class="cooktime">25 Mins<span class="value-title" title="PT0H25M"></span></span>';
$str = preg_replace('/" title="/', '" itemprop="prepTime" content="', $str);
echo $str,"\n";
output:
<span class="cooktime">25 Mins<span class="value-title" itemprop="prepTime" content="PT0H25M"></span></span>
OK - I figured out the answer. Thanks to Andrew & M42 for giving ideas. Here it is:
$line = preg_replace('/\ (.*?)class="preptime"(.*?)<span class="value-title" title(.*?)/i','$1class="preptime"$2<span class="value-title" itemprop="prepTime" content$3',$line);
$line = preg_replace('/\ (.*?)class="cooktime"(.*?)<span class="value-title" title(.*?)/i','$1class="cooktime"$2<span class="value-title" itemprop="cookTime" content$3',$line);
How can i remove the link and remain with the text?
text text text. <br><a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>
like this:
text text text. <br>
i still have a problem.....
$text = file_get_contents('http://www.example.com/file.php?id=name');
echo preg_replace('#<a.*?>.*?</a>#i', '', $text)
in that url was that text(with the link) ...
this code doesn't work...
what's wrong?
Can someone help me?
I suggest you to keep the text in link.
strip_tags($text, '<br>');
or the hard way:
preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text)
If you don't need to keep text in the link
preg_replace('#<a.*?>.*?</a>#i', '', $text)
While strip_tags() is capable of basic string sanitization, it's not fool-proof. If the data you need to filter is coming in from a user, and especially if it will be displayed back to other users, you might want to look into a more comprehensive HTML sanitizer, like HTML Purifier. These types of libraries can save you from a lot of headache up the road.
strip_tags() and various regex methods can't and won't stop a user who really wants to inject something.
Try:
preg_replace('/<a.*?<\/a>/','',"test test testa<br> <a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>");
this is my solutions :
function removeLink($str){
$regex = '/<a (.*)<\/a>/isU';
preg_match_all($regex,$str,$result);
foreach($result[0] as $rs)
{
$regex = '/<a (.*)>(.*)<\/a>/isU';
$text = preg_replace($regex,'$2',$rs);
$str = str_replace($rs,$text,$str);
}
return $str;}
A version from the above compiled notes:
$withoutlink = preg_replace('/<a.*>(.*)<\/a>/isU','$1',$String);
strip_tags() will strip HTML tags.
Try this one. Very simple!
$content = "text text text. <br><a href='http://www.example.com' target='_blank' title='title' style='text-decoration:none;'>name</a>";
echo preg_replace("/<a[^>]+\>[a-z]+/i", "", $content);
Output:
text text text. <br>
Try:
$string = preg_replace( '#<(a)[^>]*?>.*?</\\1>#si', '', $string );
Note:
this code remove link with text.
One more short solution without regexps:
function remove_links($s){
while(TRUE){
#list($pre,$mid) = explode('<a',$s,2);
#list($mid,$post) = explode('</a>',$mid,2);
$s = $pre.$post;
if (is_null($post))return $s;
}
}
?>