PHP Regex Problem?

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


23 points

Greetings,

How can I solve the problem below using PHP regex?
The text below needs to be converted:

<?php
$text 
'This is <em>nice</em>. Click <a href= "http://example.com"> here </a>';
?>

to
<?php
$text 
'This is nice. Click here (http://example.com)';
?>

Any help?



2 points

Do something like this:

<?php

//match for all text in href="" and store in m using preg_replace
preg_match_all('/href="([^"]+)"/'$text$m);

//Use array m to replace </a> with ($m) value calculated in preg_replace in $text
$text str_replace('</a>'' (' $m[1][0] . ')'$text);

// strip all html tags now
$text strip_tags($text);
?>

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.