Gadgetster
Gadgetster

Reputation: 483

PHP Remove a string that starts with http:// or www and ends with .com/.ca/.net etc

I found a bunch of link that show how to delete anything within with str_replace() but I want to censor the link on a post. Even better, somehow detect when the user is trying to post a link and not let them submit the form until they remove it

I am not to sure what to write inside str_replace() or how to check the page for inserted urls

Any help is appreciated!

Upvotes: 0

Views: 133

Answers (3)

Philip G
Philip G

Reputation: 4104

This could be achieved with regular expression. A kind of pattern comparision

Like this:

$pattern = "/(?i)(<a([^>]+)>(.+?)<\/a>)/";
$output = preg_replace ( $pattern , "Censured link",$inputText);

//assuming $inputText contains your input

this will replace all anchors with the text Censured link

Upvotes: 1

ɹɐqʞɐ zoɹǝɟ
ɹɐqʞɐ zoɹǝɟ

Reputation: 4370

you can use these functions(little bit ease)

$link1="http://www.asda.com";
$link2="http://www.asda.net";
$link3="http://www.asda.ca";
function startsWith($to, $find)
{
    return $find=== "" || strpos($to, $find) === 0;
}
function endsWith($to, $find)
{
    return $find=== "" || substr($to, -strlen($find)) === $find;
}

var_dump(startsWith($link1, "https")); // true
var_dump(endsWith($link2, "au")); 
...so on

Upvotes: 0

mikeventi
mikeventi

Reputation: 54

You'll want to check the submitted string against a regular expression using preg_match().

preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $string)

Upvotes: 1

Related Questions