max
max

Reputation: 49

regex for preg_replace() tweet url into id tag not working

I have the next string

 $string= "
https://twitter.com/AdamaUK_/status/1262730246668922885 
text
https://twitter.com/wheat_daddy/status/1262629949908885511?s=20
text
https://twitter.com/AdamaUK_/status/1262730246668922885
text
https://twitter.com/wheat_daddy/status/1262629949908885511?s=30

";

I want to preg_replace() it by

     <div id='1262730246668922885'></div> text
      <div id='1262629949908885511'></div> text
       <div id='1262730246668922885'></div> text
        <div id='1262629949908885511'></div> text

Ive tried so far without success

       $pattern = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|twitter+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";

  function getTweet($string) {
    return preg_replace($pattern, "<div id='$1'></div>", $string);
        }

How can I get the id from the twitter url and replace it successfully by a div tag? thank you.

Upvotes: 0

Views: 155

Answers (3)

AbraCadaver
AbraCadaver

Reputation: 78994

Probably lots of patterns, depends on how your brain works. I would just look for the twitter part and then get the numbers after /status/:

$result = preg_replace('~https?://twitter\.com/.*?/status/(\d+).*~', "<div id='$1'></div>", $string);

Upvotes: 1

Piet Bosch
Piet Bosch

Reputation: 166

Not sure if this is what you are looking for but I thought that

<div id='1262730246668922885'>text</div>

is better than

<div id='1262730246668922885'></div> text

The below also removes the ?s=20 as requested.

Here is the script I used to test the expressions:

<?php
$string= "
https://twitter.com/AdamaUK_/status/1262730246668922885
text
https://twitter.com/wheat_daddy/status/1262629949908885511?s=20
text
https://twitter.com/AdamaUK_/status/1262730246668922885
text
https://twitter.com/wheat_daddy/status/1262629949908885511?s=30

# Also caters for optional http, https and www:

https://www.twitter.com/AdamaUK_/status/1262730246668922885
text

http://www.twitter.com/AdamaUK_/status/1262730246668922885
text

http://twitter.com/AdamaUK_/status/1262730246668922885
text

www.twitter.com/AdamaUK_/status/1262730246668922885
text

twitter.com/AdamaUK_/status/1262730246668922885
text

";

$pattern = "~(?:https?:\/\/)?(?:www\.)?twitter\.com\/[^\/]+/status/(\d+)(?:.+)?~";
echo preg_replace($pattern, "<div id='$1'></div>", $string);


echo "Version where the text is inside the div (Just thinking <div id='1262730246668922885'>text</div> is better than <div id='1262730246668922885'></div> text)";

$pattern = "~(?:https?:\/\/)?(?:www\.)?twitter\.com\/[^\/]+/status/(\d+)(?:.+)?\R(.+)?~m";
echo preg_replace($pattern, "<div id='$1'>$2</div>", $string);

?>

Result when executed on the command line:

$ php -f test.php 

<div id='1262730246668922885'></div>
text
<div id='1262629949908885511'></div>
text
<div id='1262730246668922885'></div>
text
<div id='1262629949908885511'></div>

# Also caters for optional http, https and www:

<div id='1262730246668922885'></div>
text

<div id='1262730246668922885'></div>
text

<div id='1262730246668922885'></div>
text

<div id='1262730246668922885'></div>
text

<div id='1262730246668922885'></div>
text

Version where the text is inside the div (Just thinking <div id='1262730246668922885'>text</div> is better than <div id='1262730246668922885'></div> text)
<div id='1262730246668922885'>text</div>
<div id='1262629949908885511'>text</div>
<div id='1262730246668922885'>text</div>
<div id='1262629949908885511'></div>
# Also caters for optional http, https and www:

<div id='1262730246668922885'>text</div>

<div id='1262730246668922885'>text</div>

<div id='1262730246668922885'>text</div>

<div id='1262730246668922885'>text</div>

<div id='1262730246668922885'>text</div>

Upvotes: 1

CrazyCat
CrazyCat

Reputation: 11

Why so complicated ?

$pattern = '#https?:\/\/|www\d{0,3}[.]|twitter\.com\/([^\/]+)\/status\/(.+)#';

$1 will be the username and $2 the status id.

Upvotes: 1

Related Questions