0

I'm using the Twitter API to fetch information about all of my followers. In many cases, their bio will have a link to their Instagram page. They usually define it by writing something along the lines of IG: myigusername94 or instagram - myigusername93. I'm not that good with regex and I'd like to know how to test for the presence of a set of chars like ig: or 'instagram -` and replace it with

"ig: <a href="http://instagram.com/myigusername94">myigusername94</a>"

I know this is probably a simple fix. But, I'm very new to regex.

0

1 Answer 1

1

You can search using this regex:

\b(instagram|ig)\s*[:-]\s*\K([\w.]+)\b

And replace it by:

<a href="$1">$1</a>

RegEx Demo

Code:

$re = '/\b(instagram|ig)\s*[:-]\s*\K([\w.]+)\b/'; 

$result = preg_replace($re, '<a href="$1">$1</a>', $input);
Sign up to request clarification or add additional context in comments.

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.