1

I have created the next function to replace an url by a div with its id.

 function twitterIzer($string){
  $pattern = '~https?://twitter\.com/.*?/status/(\d+)~';

   $string = preg_replace($pattern, "<div class='tweet' id='tweet$1' tweetid='$1'></div>", $string);    

return $string;
  }

It works well when I use this type of url

 https://twitter.com/Minsa_Peru/status/1260658846143401984

but it retrieve an excedent ?s=20 when I use this url

https://twitter.com/Minsa_Peru/status/1262730246668922885?s=20

How can I remove this ?s=20 text, in order to make work my function ? Anything I know is I need to improve my regex pattern. thank you.

max
  • 49
  • 5
  • Does this answer your question? [How do I remove all specific characters at the end of a string in PHP?](https://stackoverflow.com/questions/2053830/how-do-i-remove-all-specific-characters-at-the-end-of-a-string-in-php) – Huy Trịnh May 19 '20 at 18:27
  • https://www.php.net/manual/en/function.parse-url.php – Sammitch May 19 '20 at 18:28
  • @HuyTrịnh, actually dont, because `?s=x` or `?m` the ending after the question mark is variable and its inside a preg_match, – max May 19 '20 at 18:29
  • @Sammitch I need to improve the regex only to remove everythin after the ?, including itself. – max May 19 '20 at 18:30
  • Parsing the URL into its component parts will simplify doing that. – Sammitch May 19 '20 at 18:34
  • @Sammitch could you please provide me an example? I just cant figure it out – max May 19 '20 at 18:38
  • `var_dump(parse_url($a_url));` – Sammitch May 19 '20 at 19:22

1 Answers1

0

If you want just regex:

$pattern = '/https?:\/\/twitter\.com\/.*?\/status\/(\d+)(.*)?/';

Because ? is not a digit so it will seperate with (.*), this mean every thing rest and in this case is ?s=xyz, last question mark ? is to say that is can exist or not.

Learn regex

Huy Trịnh
  • 733
  • 3
  • 11