0

I have a robot voice that turns sentences into spoken mp3 files. But the robot voice can't pronounce urls so I want to filter the urls out.

I got dynamic strings coming in that can look like this: "Hello my name is Jeffrey" This works fine but strings can also contains urls and looks like this: "Hello http://wwww.google.nl is a very nice site." or "Hello how are you doing https://soundcloud.com/theforeignexchangemusic/zo-manmade-sampler …" or "Take a look at this picture http://instagram.com/p/xPiSn8Pmli/ " And so on

If a string contains an url I want to replace the url with a word. Does anybody know a good way of doing this? Because the strings are dynamic (Length, content and location) I find it very hard to do. If someone has a good idea please let me know! Would be appreciated a lot.

3
  • You can use a regular expression that searches for URLs and using said regular expression, replace the URL with a word of your choice. Commented Dec 30, 2014 at 20:01
  • Well, I still understand your question properly. So, if it is coming like "Hello wwww.google.nl is a very nice site." then you want it to be like this "Hello Google is a very nice site."? Commented Dec 30, 2014 at 20:01
  • Yes or even "Hello a website is a very nice site." Commented Dec 30, 2014 at 20:24

2 Answers 2

1

Your best bet is to use RegEx to parse the strings to see if URLs come up.

Using RegEx to then find the base domain and then vocalize that.

/^(https?://)?([\da-z.-]+).([a-z.]{2,6})([/\w .-])/?$/ Regex for URLs

For more reference

For Parsing your URL

Sign up to request clarification or add additional context in comments.

Comments

0

If somebody is looking for exactly the same thing here is a working example.

<?php
$hallo = 'Hey it works. http://onderbroekenlol.nl something behind the url.';
$str = preg_replace("/(?:https?:\/\/)?(?:[\w]+\.)([a-zA-Z\.]{2,6})([\/\w\.-]*)*\/?/", "a website", $hallo);
print $str;
?>

1 Comment

[\w] is more simply written as \w. Inside of a character class, . does not need to be escaped. If you have a pattern with / in it, it is simpler to use a unique character as the pattern delimiter such as ~ so that you don't have to escape all of the / characters.

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.