0
Tried this,

$url = preg_replace('!(http|http:|ftp|scp)(s)?:\/\/[a-zA-Z0-9.=?&-_/]+!', "<a style=text-decoration:underline;color:blue href=\"\\0\" target='_blank' >\\0</a>",$feed->feed_text)):

I want to replace both http and http: from the url. I tried the above one which is replacing the http and not http:.But if i tried any one of then in regex it is working.If i use both in the expression it is not replacing the http:

Please see the embeded script i am using,

1.<iframe frameborder="0" scrolling="no" id="chat_embed" src="http://twitch.tv/chat/embed?channel=athenelive&popout_chat=true" height="500" width="350"></iframe> ->having http:(need to remove)

2.<iframe width="560" height="315" src="//www.youtube.com/embed/JxHOjZLRxAg" frameborder="0" allowfullscreen></iframe> ->if i use http: alone in regex this youtube embedded script is not taken as youtube url. For both compatible purpose i used http|http:.

Update:

I need to remove http: from the 1st frame given above.

Should look line

<iframe frameborder="0" scrolling="no" id="chat_embed" src="//twitch.tv/chat/embed?channel=athenelive&popout_chat=true" height="500" width="350"></iframe>
4
  • Your preg_replace replaces urls with urls embedded in a tags... how do you get to an iframe from that? Commented Apr 4, 2014 at 13:26
  • @Robin I tried hardcoded the value with removed http: i am getting the frame Commented Apr 4, 2014 at 13:35
  • I'm sorry, I don't understand what that means. Your code in the preg_replace is (should be) replacing http://sth.org with <a ...>http://sth.org</a>. What's the link with an iframe? Commented Apr 4, 2014 at 13:43
  • Please give an example of input and desired output. Commented Apr 4, 2014 at 13:52

4 Answers 4

2

It doesn't work for http: because you put a colon after the capturing group:

(http|http:|ftp|scp)(s)?:
          ^             ^

To solve the problem, remove the http: from the capturing group (that is useless):

(http|ftp|scp)(s)?:

I think your are looking for that:

$url = preg_replace('~\bhttps?:~', '', $url);
Sign up to request clarification or add additional context in comments.

3 Comments

initially i tried your solution but that too not escaping the http: -->preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z
Can you tell me how to replace http: in this expression this is to save the embeded script in db ->preg_match('/<iframe.*src=\"(.*)\".*/isU', $_POST['embed_url'], $embed_string);
@user2439275: But what are you trying to do? Your goal seems more and more obscure. Are you trying to find all the iframe in an html code and then store these whole iframe tags to db after removing http: from the src attribute?
0

Replace (http|http:) with

http:?

When in an alternation, the regex engine tries the matches in order. In http://sthg.com, http matches so it has no reason to try the http: variant. (using (http:|http) would actually work for you)

Using the greedy :? will solve the issue.

Also, to improve your regex, the parenthesis around (s)? are useless (s? is enough) and / is not a special character if you don't use it as a regex delimiter (here you're using !) so no need to escape them.

1 Comment

tried this preg_replace('!(http:?|ftp|scp)(s)?:\/\/[a-zA-Z0-9.=?&-_/] ->not working
0

If you want to strip http:// just do the following :

$url = str_replace('http:', '', $my_url);

4 Comments

Updated. Straightest solution, why use a complicated regular expression, when you already know you don't need a precise sequence of character ?
... because it does need a precise sequence of characters ... either http or http: (or, looking at the question also ftp or scp - potentially with the implication that there may or may not be colons there as well) - granted you're never going to get an URL that starts http// (without the colon) :)
Oh ... realistically it's also got to be only at the start of the string as well so that you don't break URLs like http://www.fred.com/http:/lovelyhttp:code.html - with str_replace you'd need to provide the count parameter str_replace('http:', '', $feed->feed_text, 1) which is fine unless the URL happens to be //www.fred.com/http:/lovelyhttp:code.html in the first instance ... in which case you're going to break the URL again.
Looking at the request the OP asks for "http" to be replaced (he probably took the regular expression from somewhere and copy-pasted it). I assumed he's only going to take service-generated urls to embed players, so I omitted the count parameter (though it was needed in such case), by the way +1 for the second case, I hadn't thought of that.
0

As I understand the question - you might be better off just removing everything before the initial '//' - it looks as though you want the URLs to appear as //www.somewhere.com/something/something/darkside/something/ - so using a RegExp like this might suit your purposes better:

$url = preg_replace('|^[^/]*(//.*)$|', "$1", $feed->feed_text);

Comments

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.