0

Say I have values on my page, like #100 #246, What I want to do is scan the page for values with a # before them and then alter them to put a hyperlink on it

$MooringNumbers = '#!' . $MooringNumbers . ' | ' . '#!' . $row1["Number"];
  }  
    $viewedResult =  '<tr><td>' .$Surname.'</td><td>'.$Title.'</td><td>'.$MooringNumbers . '</td><td>'.$Telephone.'</td><td>' . '<a href="rlayCustomerUpdtForm.php?id='.$id.'">[EDIT]</a></td>'.'<td>'. '<a href="deleteCustomer.php?id='.$id.'">[x]</a></td>'. '</tr>';   preg_replace('/#!(\d\d\d)/', '<a href="update.php?id=${1}">${1}</a>', $viewedResult);
    echo $viewedResult;

This is the broken code which doesnt work.

Jørgen R
  • 10,568
  • 7
  • 42
  • 59
DogPooOnYourShoe
  • 149
  • 3
  • 7
  • 14

2 Answers2

2

I second Xoc - use PHP manual. The method next to the one he pointed is preg-replace-callback

Just call:

preg_replace_callback(
        '/#\d\d\d/',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
            'return strtolower($matches[0]);' //this you replace with what you want to fetch from database
        )

EDIT: Since you want to always perform the same replacement go with Xoc's preg-replace:

preg_replace('/#!(\d\d\d)/', '<a href="update.php?id=${1}">${1}</a>', $your_input);

Note: I don't have PHP here, so I give no guarantee of this code not wiping your entire hard disk ;)

Majki
  • 618
  • 7
  • 14
0

You can accomplish this by using regular expressions, see PHP's preg_replace function.

$text = 'Lorem ipsum #300 dolar amet #20';

preg_match_all('/(^|\s)#(\w+)/', $text, $matches);

// Perform you database magic here for each element in $matches[2]
var_dump($matches[2]);

// Fake query result
$query_result = array ( 300 => 'http://www.example1.com', 20 => 'http://www.example2.com');

foreach($query_result as $result_key => $result_value)
{
    $text = str_replace('#'.$result_key, '<a href="'.$result_value.'">'. $result_value . '</a>', $text);
}

var_dump($text);
joelcox
  • 562
  • 9
  • 19
  • My values are random, and I do not know what all of them will be as they are pulled down from a database. That page just shows how to do it if I knew what the values were. – DogPooOnYourShoe Dec 31 '10 at 10:51
  • IMHO this articles are relevant and completely related http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – markcial Dec 31 '10 at 10:53
  • So each #integer has a URL stored in the database? I would write a regex to find all #s, put the integers in an array, query your database and perform a str_replace() for each array element. – joelcox Dec 31 '10 at 11:03
  • I added a code example to my original answer that uses the algorithm I described. – joelcox Dec 31 '10 at 11:28
  • $query_result = array ( 300 => 'http://www.example1.com', 20 => 'http://www.example2.com'); That is the bit that I dont want to use, as my values are random and only need to be submitted like – DogPooOnYourShoe Dec 31 '10 at 11:34
  • Is there any way to automatically convert the Found String with # Before it, to a URL? – DogPooOnYourShoe Dec 31 '10 at 11:36