Skip to content

Instantly share code, notes, and snippets.

@sabbir1991
Created January 5, 2023 07:05
Show Gist options
  • Save sabbir1991/1259ea8f06956d847366d55b9266553e to your computer and use it in GitHub Desktop.
Save sabbir1991/1259ea8f06956d847366d55b9266553e to your computer and use it in GitHub Desktop.
Replace anchor tag content depending on specific attributes.
$input_lines = '<a href="index.php" target="_blank" class="link testing" >Test Link</a><a href="index.php" class="link testing" target="_blank" >Test Link</a><a href="index.php" target="_blank" class="link testing" >Test Link</a>';
$replace_text = preg_replace('/(\<a.*?target=\"_blank\".*?>)(.*?)(\<\/a\>)/', '$1$2<span>screen reader</span>$3', $input_lines);
echo $replace_text;
// Output...
<a href="index.php" target="_blank" class="link testing" >Test Link<span>screen reader</span></a>
<a href="index.php" class="link testing" target="_blank" >Test Link<span>screen reader</span></a>
<a href="index.php" target="_blank" class="link testing" >Test Link<span>screen reader</span></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment