2
    $text = "<p>Pellentesque ac urna eget diam volutpat suscipit
     ac faucibus #8874# quam. Aliquam molestie hendrerit urna, 
    vel condimentum magna venenatis a.<br/> Donec a mattis ante. 
Ut odio odio, ultrices ut faucibus vel, #2514# dapibus sed nunc. In hac sed.</p>";

I need to search for #myid# string, and replace with <a href='mylink.php?myid'>myid</a>

How can I do?

1
  • 1
    str_replace but i don't like it... Commented May 5, 2011 at 15:45

2 Answers 2

4

Why do you need regular expressions to do this, or are you planning to replace all 4 digit numbers (surrounded with #)??

If it's for a single ID, you could use:

$replace_id = 2514;

$replacement_find = "#{$replace_id}#";
$replacement_replace = "<a href=\"mylink.php?{$replace_id}\">{$replace_id}</a>";

$text_final = str_replace($replacement_find, $replacement_replace, $text);

If you're doing multiple replacements (or, as per comment, not knowing any IDs) you can use this:

$text_final = preg_replace('@#([0-9]+?)#@', '<a href="mylink.php?$1">$1</a>', $text);

EDIT: From your comment, you could use preg_match_all to get all IDs in an array that are contained within the string. In the following code, $matches[1] contains the value of everything within the ()s in the regular expression, see return of print_r.

preg_match_all('@#([0-9]+?)#@', $text, $matches);
print_r($matches[1]);

To accommodate your latest request, you can use preg_replace_callback to get what you want:

function callback($matches) {
        return '<a href="mylink.php?' . $matches[1] . '>' . getProductName($matches[1]) . '</a>';
}
$text_final = preg_replace_callback('@#([0-9]+?)#@', "callback", $text);
Sign up to request clarification or add additional context in comments.

7 Comments

But he may not know the IDs in advance
Updated my answer with a simpler regex than Duniyadnd's to get the correct result, commented Duniyadnd's answer.
The preg_replace will automatically find/replace all matches. If you want to get them into an array you could use preg_match_all, yes: preg_match_all('@#([0-9]+?)#@', $text, $matches); print_r($matches[1]);
and instead <a href="mylink.php?$1">$1</a> i need to get <a href="mylink.php?$1"> getNameProduct(myid) </a> ?? Is it possible??
I've updated my answer. You need to use preg_replace_callback in this case!
|
3
$text = preg_replace("/\#(\d+?)\#/s" , "<a href=\"mylink.php?$1\">$1</a>" , $text);

6 Comments

This will pull through the #s to the link aswell, fixed/alternate regex depending on what the OP requires is in my answer.
Ha - nice, I was updating it when your comment came in. Upvoted your comment. :)
@Duniyadnd, preg_match (if used) would actually only find the first occurrence of the regex then stop, preg_match_all will match them all. preg_replace will replace all occurrences like you said.
@rudi_visser - I'm an idiot, i was thinking preg_replace() while I was typing preg_match().
and instead <a href="mylink.php?$1">$1</a> i need to get <a href="mylink.php?$1"> getNameProduct(myid) </a> ?? Is it possible??
|

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.