How to remove links from a PHP string using preg_replace - php

I'm using a chat bot script, if a user name was test#test.com the bot will reply # <a href= mailto:test#test.com>test#test.com</a> with a mailto link. I want the reply to be only test#test.com without the link, I tried preg_replace and str_replace but I don't really know the exact code to use, I've tried the following but didnt work !
$name = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $name);
The whole code I'm using for replacements is this:
$name = str_replace (chr(0xc2).chr(0xa0), "_", $name);
$name = str_replace ("'", "", $name);
$name = str_replace (""", '"', $name);
$name = str_replace ("&", "&", $name);
$name = str_replace ("<", "", $name);
$name = str_replace (">", "", $name);
$name = str_replace ("&", "_", $name);
$name = str_replace ("*", "_", $name);
$name = preg_replace('/[^ \p{L}\p{N} \# \_ \- \.\#\$\&\!]/u', '', $name);
$name = preg_replace('/<a href="([^<]*)">([^<]*)<\/a>/', '', $name);

Why do you want to replace it? Just use preg_match() with a regex similar to this:
<a href=[^>]+>([^<]*)</a>
so overall your code would look like this
<?php
$regex = '#<a href=[^>]+>([^<]*)</a>#';
$email = '<a href= mailto:test#test.com>test#test.com</a>';
preg_match($regex, $email, $matches);
var_dump($matches[1]);
/*
output:
string(13) "test#test.com"
*/
?>

The answer above makes a lot of assumptions when doing the preg_replace so it's going to fail lots unfortunately :( Here's why...
It assumes every link has the 'href' attribute directly after the 'a' tag. What if there's a different attribute in front of it?
It assumes there are no other html tags inside the 'a' tag. If the link had the 'strong' tag inside it, the link would not be matched.
I'm pretty sure too that if there's more than one link in the list it's going to remove everything between the first link and the second because it hasn't got anything to stop it being greedy.
Finally, it's not been told to be insensitive. This means that if the link had A HREF in it, that wouldn't be found either.
I'm not saying my solution is 100% secure but I've tested it in scenarios I'm aware of and I think it's an upgrade from the answer above!...
$email = preg_replace("/<a.+?href.+?>.+?<\/a>/is","",$email);
The 'i' modifier makes it insensitive
The 's' modifier takes into account links that might be broken with newline breaks.
I'd always recommend populating a string with different links in different formats, different orders etc. That's always the best way to test things work. Assuming eveyone types links as My test is going to get you into lots of sticky situations :)
Good luck!

Related

How to replace a portion of string with a dot in regex?

Here is the string that I want to replace:
<img src="./handler_image.php?i=c52bc1c30f560f4a15f99eeb8c04fea6" alt="Favicon" class="favicon">
I wrote this code:
$answer = preg_replace('/<img src="\./.*?>/', '', $answer);
but it does not work. If I replace it with:
$answer = preg_replace('/<img src=".*?>/', '', $answer);
It works but then it replaces all the images and not the ones whose src is in the format above. How should I modify this statement?
The forward slash after the dot needs to be escaped too.
try this:
$answer = preg_replace('/<img src="\.\/.*?>/', '', $answer);
Here's the regex working <img src="\.\/.*?>.
By the way, you can go there http://regexr.com/ to know how your regex is working.

How to remove only links from string and nothing more

I have string: Some text (some text) anylink.com www.anylink.com http://anylink.com R.I.O one-www.link.com
I have this code:
$pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
$replacement = "";
$name = preg_replace($pattern, $replacement, $name);
It removes fine: anylink.com, www.anylink.com, http://anylink.com
But its also remove word R.I.O and one- how I can avoid this?
Thanks for help.
I believe that you should be sure of what is really a link or not
you need to find a pattern
I realize that all your links may end with .com
so try this regex "[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+.com$"
but if you need .com.anything or .com/anything
"[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+.com.*$"

Ignore specific character: -

I want to ignore a specific character using php. So when a user adds this character in the textbox. the php scripts filters it out first. I tried something and came up with this:
<?php
$datetogoto = $_GET['datetogoto'];
$pattern = '-';
$replace = '';
preg_replace($pattern, $replace, $datetogoto);
header('Location: ../index.php?newsdate='.$datetogoto);
?>
So what it wrong with this code?
Can you try using str_replace
$datetogoto = $_GET['datetogoto'];
$datetogoto = str_replace("-","", $datetogoto);
Ref: http://us1.php.net/str_replace
Or , if you want get date format whatever you sent in query string, then use urlencode()
header('Location: ../index.php?newsdate='.urlencode($datetogoto));
PHP regex needs delimiters, so use it like this:
$pattern = '/-/';
OR else use str_replace:
str_replace('-', $replace, $datetogoto);

Remove text inside of text from a larger string with PHP

What I'm trying to do is, if it exists, remove an occurrence of text inside a 'shortcode', eg: Here's some content [shortcode]I want this text removed[/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content.
It seems like a pretty simple thing to do but I can't figure it out.. =/
The shortcode will only show up once in the entire string.
Thanks in advance for help.
Try this:
$var = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content";
$startTag = "[shortcode]";
$endTag = "[/shortcode]";
$pos1 = strpos($var, $startTag) + strlen($startTag);
$pos2 = strpos($var, $endTag);
$result = substr_replace($var, '', $pos1, $pos2-$pos1);
It's very easy to do with preg_replace(). For your purpose, use /\[shortcode\].*\[\/shortcode\]/ as pattern.
$replace = "[shortcode][/shortcode]";
$filteredText = preg_replace("/\[shortcode\].*\[\/shortcode\]/", $replace, $yourContent);
See http://php.net/manual/en/function.preg-replace.php for more details.
One can use strpos() to find the position of [substring] and [/substring] in your string and replace the text with a whitespace via substr_replace()
if you do not want to bother with regular expessions:
if you do have the [shortcode] tag inside the string, than it is really no problem: just use a nested use of substr:
substr($string,0,strpos($string,'[substring]')+11)+substr($string,strpos($string,'[/substring]'),strlen($string))
where the first substr cuts the string to the start of the string to cut and the second adds the remaining stuff of the string.
see here:
http://www.php.net/manual/en/function.substr.php
http://www.php.net/manual/en/function.strpos.php
use regex in php to get rid of it.
preg_replace (shortcode, urText, '', 1)
$string = "[shortcode]I want this text removed[/shortcode]";
$regex = "#\[shortcode\].*\[\/shortcode\]#i";
$replace = "[shortcode][/shortcode]";
$newString = preg_replace ($regex, $replace, $string, -1 );
$content = "Here's some content [shortcode]I want this text removed[/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content";
print preg_replace('#(\[shortcode\])(.*?)(\[/shortcode\])#', "$1$3", $content);
Yields:
Here's some content [shortcode][/shortcode] Some more content to be changed to Here's some content [shortcode][/shortcode] Some more content

How to change all non word characters and multiple spaces into ' ' and then all space to '-' in one preg_replace()

I want to change image names on below conditions
All non word characters turns into space and
then all spaces turns into -
means if my image name is : Hello My name is'Kh#n "Mr. .Khan " then it should be changed into Hello-My-name-is-kh-n-mr-Khan .
i need to use below in two steps,
$old_name =' Hello My name is\'Kh#n "Mr. Khan ';
$space_name = preg_replace('/\W/',' ', $old_name);
$new_name= preg_replace('/\s+/','-', $space_name);
echo $new_name // returns Hello-My-name-is-kh-n-mr-Khan
is there any way to apply both conditions in single step??
preg_replace can take arrays:
$new_name = preg_replace(array('/\s+/', '/\W/'), array('_', '-'), $old_name);
This is more succinct, but I don't believe it's any more efficient. The docs don't specify an order that the regexps are applied. Since space characters are non-word characters, a safer version is:
$new_name = preg_replace(array('/\s+/', '/[^\s\w]/'), array('_', '-'), $old_name);
I find this function's output (Hello_My_name_is-Kh-n_-Mr-_Khan_)a bit ugly
Here is my approach
$name ='Hello My name is\'Kh#n "Mr. Khan" ';
$name = preg_replace('/\W/',' ', $name);
$name = trim($name);
$name = strtolower($name);
$name = preg_replace('/\s+/','-', $name);
outputs
hello-my-name-is-kh-n-mr-khan
You can use arrays in preg_replace:
$old_name ='Hello My name is\'Kh#n "Mr. Khan ';
$new_name = preg_replace(array('/\s+/','/\W/'),array('_','-'),$old_name);
Also,
Your snippet has syntax error, you need to escape the single quote before K
Is this to rename uploaded images? If so, keep in mind...
max char count (should snip it at something sensible)
non ASCII chars? (transliterate would be best)

Categories