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.*$"
Related
I want to replace a section of a string based that starts with one string and ends with another, and I want the section between also replaced. I think this is possible using regex but I cant' seem to find any decent examples showing this.
For Example:
I have "http://www.website.com" and I want to replace from "www" to "com" with "123xyz".
So"http://www.website.com/something" becomes "http://123xyz/something.
I am assuming I have to use preg_replace(), and I think the regex should start with "^www" and end with "com$", but I cant seem to get a grasp of the syntax of regex enough to create the desired effect.
please help
With reference to your example , you can try like this
$string = 'http://www.website.com/something';
$pattern = '/www(.*)com/';
$replacement = '123xyz';
echo preg_replace($pattern, $replacement, $string);
$phrase = "http://www.website.com";
$phraseWords = array("www", "com");
$replaceTo = array("123xyz", "something");
$result = str_replace($phraseWords, $replaceTo, $phrase);
echo $result;
Thanks so much to both #CodingAnt and #PHPWeblineindia for your great answers. Using #CodingAnt's answer (and some more research I did online) I wrote this function:
function replaceBetween(&$target, $from, $to, $with){
if(strpos($target, $from)===false)return false;
$regex = "'".$from."(.*?)".$to."'si";
preg_match_all($regex, $target, $match);
$match = $match[1];
foreach($match as $m) $target = str_replace($from.$m.$to, $with, $target);
return $target;
}
It seems to work pretty well. I hope someone finds this useful.
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!
I am not the best with RegEx... I have some PHP code:
$pattern = '/activeAds = \[(.*?)\]/si';
$modData = preg_replace($pattern,'TEST',$data);
So I have a JavaScript file, and it declares and array:
var activeAds = [];
I need this to populate the array with my string, or if the array already has a string inside it, i want to replace it with my string (in this case "TEST").
Right now, my REGEX is replacing everything, including my start and end, i need to only replace whats between.
I'm left with:
var TEST;
TIA
You could capture what's before and what's after the part you want replacing:
$pattern = '/(activeAds = \[).*?(\])/si';
After capturing these parts, you can keep them and replace the part in the middle:
$modData = preg_replace($pattern, '\1TEST\2', $data);
There are many ways you could do this, mine is below:
$data = array("activeAds = testing123");
$pattern = "/activeAds\s?=\s?(.*)/";
$result = preg_replace($pattern,"activeAds = TEST", $data);
var_dump($result);
Edit: Forgot to mention that the \s? here allow for an optional space.
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
I need to strip a URL using PHP to add a class to a link if it matches.
The URL would look like this:
http://domain.com/tag/tagname/
How can I strip the URL so I'm only left with "tagname"?
So basically it takes out the final "/" and the start "http://domain.com/tag/"
For your URL
http://domain.com/tag/tagname/
The PHP function to get "tagname" is called basename():
echo basename('http://domain.com/tag/tagname/'); # tagname
combine some substring and some position finding after you take the last character off the string. use substr and pass in the index of the last '/' in your URL, assuming you remove the trailing '/' first.
As an alternative to the substring based answers, you could also use a regular expression, using preg_split to split the string:
<?php
$ptn = "/\//";
$str = "http://domain.com/tag/tagname/";
$result = preg_split($ptn, $str);
$tagname = $result[count($result)-2];
echo($tagname);
?>
(The reason for the -2 is because due to the ending /, the final element of the array will be a blank entry.)
And as an alternate to that, you could also use preg_match_all:
<?php
$ptn = "/[a-z]+/";
$str = "http://domain.com/tag/tagname/";
preg_match_all($ptn, $str, $matches);
$tagname = $matches[count($matches)-1];
echo($tagname);
?>
Many thanks to all, this code works for me:
$ptn = "/\//";
$str = "http://domain.com/tag/tagname/";
$result = preg_split($ptn, $str);
$tagname = $result[count($result)-2];
echo($tagname);