0

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!

3
  • 1
    You will likely need to look at regular expressions (regex) to do this. Commented Mar 20, 2014 at 17:25
  • @Gadgetster it may be helpful if you can provide an example of what you're looking for. Commented Mar 20, 2014 at 17:26
  • @KyleChallis I thought it was straight forward.. an example: most forums delete or censor urls as they don't want external links on their site Commented Mar 20, 2014 at 20:40

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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

7 Comments

<a [.]*</a> will not do what you think it does. You probably want a lazy match: <a .*?</a> — but this is not guaranteed to work for all possible cases, though.
Pattern is changed. But whats wrong with the above?
[.] will not match any character. It will only match the literal period character (.).
Thats why I changed the code above. Was a bit to fast to post.
is this going to find a link on the page as well as a plain text url? I want to completely make sure there is no external url on the post
|
0

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

3 Comments

how do I feed in url's that the user will input?
once if you have the url which you want to check then you can pass it as a argument to the functions startsWith($yoururl) and endsWith($yourURL) which will return true or false.then do what you want to
can you show me what you mean in an EDIT of your answer? I don't quite understand how to check the user's input url

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.