Finding and updating text between parentheses with PHP4

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • watershed
    New Member
    • Mar 2007
    • 1

    #1

    Finding and updating text between parentheses with PHP4

    In PHP4, how would I go about finding *every* instance of text (max of 14 characters) in a string that is placed within parentheses? And then loop through each instance and update the string accordingly.

    Here's an example:

    $myString = "Google's (GOOG) YouTube, Microsoft (MSFT) MSNBC and Viacom (VIA)";

    The idea would be to take each stock symbol and insert a link like:

    (<a href="http://finance.yahoo.c om/q?s=goog">GOOG</a>)

    ...and then to do the same thing for the other symbols.

    Any pointers would be appreciated.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Try this one. You can adapt it yourself to remove the parentheses from the result array.
    [php]
    <?php
    $text = "Google's (GOOG) YouTube, Microsoft (MSFT) MSNBC and Viacom (VIA)";
    $pattern = '(\((?:[^\(\)]+(?:"[^"]*"|\'[^\']*\')?)+\))';
    preg_match_all( $pattern, $text, $snip);
    echo '<pre>'; print_r($snip);
    ?>[/php]

    Ronald :cool:

    Comment

    Working...