So I have the following method that I'm calling $text = $this->get('text')
that returns the following items:
Uploading a photo to test the Twitter API endpoint. pic.twitter.com/i4bV2UWkBM
Is there a way that I can take that return and find all pic.twitter.com
instances and replace it with an empty string or remove the URL?
I tried to use:
$text = str_replace(
'pic.twitter.com',
'',
$text
);
But it's not working, I guess the wildcard isn't properly detecting the pic.twitter.com.
CodePudding user response:
You can create a regex pattern and use preg_replace()
to remove the whole URL from the string:
<?php
$replaced = preg_replace(
'/pic\.twitter\.com\/[a-zA-z0-9]{10}/',
'',
<<<TESTS
Uploading a photo to test the Twitter API endpoint.
pic.twitter.com/i4bV2UWkBM …
pic.twitter.com/i1bx2U55BM …
pic.twitter.com/i22x2U55BM …
TESTS
);
echo $replaced;
That code will print the following:
Uploading a photo to test the Twitter API endpoint.
…
…
…