Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:53:21+00:00 2026-05-15T00:53:21+00:00

Consider the following situation: I want to replace links in a string, specifically I

  • 0

Consider the following situation: I want to replace links in a string, specifically I want to add a “search engine” link next to it.

Think: Link Title (S), where as S will link to Google with “Link Title” as search term. I’ve copied a real life example string ($content) in the code so you can reproduce this 1:1 in PHP code.

// PCRE case-insensitive, all as a single string, ungreedy and evaluate PHP replace.
$content = '<h3 class="bgg2" style="padding: 4px 0px 4px 5px; font-size: 11px;">» <a href="/forum_detail.html?topic=3456&amp;start=20&amp;post=97145#p97145" class="nub" title="Xenoblade: Japanischer TV-Spot"><b>Xenoblade: Japanischer TV-Spot</b></a></h3>';

$replace = preg_replace('/(<a.*>)(.*)<\/a>/isUe', ('"\1\2</a>&nbsp;(<a href=\"http://www.google.com/search?q=' . strip_tags(strtoupper('blah\2')) . '\">S</a>)"'), $content);

print($replace);

Output (incorrect):
Xenoblade: Japanischer TV-Spot (S) -> when you look at the HTML it looks like this:

<a href="http://www.google.com/search?q=BLAH%3Cb%3EXenoblade:%20Japanischer%20TV-Spot%3C/b%3E">S</a>

The strtoupper() did get the literal string blah -> BLAH but not the \2 back reference from the regular expression?

It looks like the \2 back referenced string is used BEFORE strtoupper() or strip_tags() functions are executed – some kind of timing of evaluation versus functions in PHP perhaps?

Does anybody know how to explain this bevhaviour?

—

I’ve developed a workaround using preg_replace_callback, still I’m puzzled about why the above code doesn’t work as I expected.

For reference to see what I want to achive:

Solution

// I have to use PHP < 5.0 so create_function() will do the job.
$replace = preg_replace_callback('/(<a.*>)(.*)<\/a>/isU',
create_function('$matches', 'return $matches[1] . $matches[2] . \'</a>&nbsp;(<a href="http://www.google.com/search?q=\' . strip_tags(strtoupper($matches[2])) . \'">S</a>)\';'),
$content);

print($replace);

Output (correct):
Xenoblade: Japanischer TV-Spot (S) -> when you look at the HTML it looks like this:

<a href="http://www.google.com/search?q=XENOBLADE:%20JAPANISCHER%20TV-SPOT">S</a>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:53:22+00:00Added an answer on May 15, 2026 at 12:53 am

    Change

    '"\1\2</a>&nbsp;(<a href=\"http://www.google.com/search?q=' . strip_tags(strtoupper('blah\2')) . '\">S</a>)"'
    

    to

    '"\1\2</a>&nbsp;(<a href=\"http://www.google.com/search?q=" . strip_tags(strtoupper(\'blah\2\')) . "\">S</a>)"'
    

    The strip_tags() function needs to be a part of the replacement string so it doesn’t get evaluated before it gets passed to preg_replace().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following situation where I have a list of n matrices (this is
Consider the following situation. I have 3 tables in a resource planning project(created using
Consider the following situation: Class** array = new Class*[8]; array[1] = new Class(1,2); Is
Consider the following situation. -(void) foo { Object * obj = [[Object alloc] init];
Consider the following query: SELECT domain, done FROM tasks WHERE 1 GROUP BY domain
Please pardon me for a n00bish question. Please consider the following code: public class
I support SQL Servers 2000 thru 2008R2 (all Standard Edition). Question 1 Please consider
I have a situation where I need to find time spans between value changes.
I have a situation where I need to assign an appropriate rate to an
You know that scene in Goodbye and Thanks For All The Fish where Arthur

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.