-1

I have $stringF. Contained within $stringF is the following (the string is all one line, not word-wrapped as below):

http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=
AFQjCNHWQk0M4bZi9xYO4OY4ZiDqYVt2SA&clid=
c3a7d30bb8a4878e06b80cf16b898331&cid=52779892300270&ei=
H4IAW6CbK5WGhQH7s5SQAg&url=https://abcnews.
go.com/Lifestyle/wireStory/latest-royal-wedding-thousands-streets-windsor-55280649

I want to locate that string and make it look like this:

https://abcnews.go.com/Lifestyle/wireStory/latest-royal-
wedding-thousands-streets-windsor-55280649

Basically I need to use preg_replace to find the following string:

http://news.google.com/news/url?sa= ***SOME UNKNOWN CONTENT*** &url=http

and replace it with the following string:

http

I'm a little rusty with my php, and even rustier with regular expressions, so I'm struggling to figure this one out. My code looks like this:

$stringG = preg_replace('http://news.google.com/news/url?sa=*&url=http','http',$stringH);

except I know I can't use wildcards and I know I need to specially deal with the special characters (colon, forward slash, question mark, and sign, etc). Hoping someone can help me out here.

Also of note is that my $stringF contains multiple instances of such strings, so I need the preg_replace to be not greedy - otherwise it will replace a huge chunk of my string unnecessarily.

  • Possible duplicate of [Get content between two strings PHP](https://stackoverflow.com/questions/1445506/get-content-between-two-strings-php) – Wiktor Stribiżew Jun 02 '18 at 20:40

1 Answers1

0

PHP has tools for that, no need to use a regex. parse_url to get the components of an url (scheme, host, path, anchor, query, ...) and parse_str to get the keys/values of the query part.

$url = 'http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=AFQjCNHWQk0M4bZi9xYO4OY4ZiDqYVt2SA&clid=c3a7d30bb8a4878e06b80cf16b898331&ci=52779892300270&ei=H4IAW6CbK5WGhQH7s5SQAg&url=https://abcnews.go.com/Lifestyle/wireStory/latest-royal-wedding-thousands-streets-windsor-55280649';

parse_str(parse_url($url, PHP_URL_QUERY), $arr);

echo $arr['url'];
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • That works as you have coded it, but how do I embed that into a find-and-replace function? My string has multiple URL addresses in it along with other content. I need to strip each and every URL address contained within the string as I described above. – Brandon Wolgast May 19 '18 at 23:01
  • 1
    @BrandonWolgast: 1) write a dumb pattern like `https?://\S+` that you will use with `preg_replace_callback`, 2) as replacement parameter, use my code to build the callback function. – Casimir et Hippolyte May 19 '18 at 23:25
  • I'm a coding newbie, you pretty much lost me. But thanks anyways, I did finally get what I wanted using regular expressions. $stringG = preg_replace('#http://news.google.com/news/.*?url=http#','http',$stringF); – Brandon Wolgast May 19 '18 at 23:59