Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2002

    [help needed] preg_replace issue with arrays

    I have this bit of code that I would like for it to search and replace a bit of text and change specific text into links.

    The code looks for [char=character name] and replaces the code with a link to the person's profile page. The problem is that if there is more than one occurrence of the "[char]" code in a field of text, it only replaces with the first occurrence. I suspect that I need to do another loop. However, as of right now this code runs on 54 fields of data on each page load and I'm hesitant about yet another loop (it will be relocated to the add/edit text function of the script once it works).

    database structure:
    id = character id, autoincrement and used in the links.

    this variable is any string of text that the function will look for names of characters in the database, and dynamically replaces the [char] code with the character name and a link to their page.

    Here is my code:
    PHP Code:
    function charLink($input) {
    $input htmlentities($inputENT_QUOTES);

    preg_match_all('/\[char=(.*?)\]/i'$input$match)) {
    $patterns[] = "#\[char=(.*?)\](.*?)#is";

            while (list (
    $key$val) = each ($match[1])) {
    $statussql "SELECT id FROM ".$prefix."_rpg_characters WHERE birthname='$val' OR nickname='$val'";
    $stchar $db->sql_fetchrow($db->sql_query($statussql));

    $stchar['id']) {
    $replacements[] = "<a href=\"mylink.php&cid=".$stchar['id']."\">$val</a>";
                } else {
    $replacements[] = "&quot;$val&quot;";

    $output preg_replace($patterns$replacements$input);

        } else {

    Here are some examples of what its doing and what I want...

    Quote Originally Posted by input that actually fails
    Sadel is the younger sister of [char=Verde Hysa], and [char=Saul Hysa]
    The function returns...
    Quote Originally Posted by wrong data
    Sadel is the younger sister of Verde Hysa, and Verde Hysa
    This is what it is supposed to return...
    Quote Originally Posted by what its supposed to say
    Sadel is the younger sister of Verde Hysa, and Saul Hysa
    Thanks for any help.

  2. #2
    Join Date
    May 2009
    Start print_r()'s of $patterns and $replacements right before you execute the preg_replace. If you're not seeing what you expect, work your way backwards, looking at $stchar and $match.

  3. #3
    Join Date
    Jan 2002
    $patterns is a single item:

    I suspect its because it is the only item in the array and as a result there isnt a matching $pattern once preg_replace has already processed the first one.

    $replacements has 2 values in it.

    Anyway I thought of something while at work and when I got home, I started messing with this script before replying to this post and managed to get it working. Thanks for the pointer, and I was doing that very thing before coming back to check on this post.

    Here's what I did to get it working...

    I changed my $output line to:
    PHP Code:
            $output preg_replace($match[0], $replacements$input); 
    This made the script operate correctly except it kept the brackets around the code, so the resulting output had.... [character name]

    I was able to remove the brackets in the output by using str_replace and adding it below my output line.

    PHP Code:
            $output str_replace(array("]","["), ""$output); 

Similar Threads

  1. PHP: I ask for a preg_replace explanation
    By SNOWmanx in forum Programming Discussion
    Replies: 5
    Last Post: 12-21-2006, 12:02 AM
  2. preg_replace error..Help..!!
    By killapix in forum Programming Discussion
    Replies: 2
    Last Post: 08-20-2006, 05:09 PM
  3. [PHP] preg_replace URLs
    By Kauwgomballen in forum Programming Discussion
    Replies: 2
    Last Post: 06-22-2005, 09:07 AM
  4. [PHP] Sorting Arrays of Associative Arrays?
    By Barti1987 in forum Programming Discussion
    Replies: 0
    Last Post: 10-23-2004, 10:50 AM
  5. Need to change <P> to nothing using preg_replace...
    By crEA-tEch in forum Programming Discussion
    Replies: 11
    Last Post: 01-29-2004, 09:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts