need php function to happen just once

jlknauff

New member
Aug 25, 2008
237
1
0
This function will run in Wordpress, but I need for it to happen just once per post

PHP:
<?php

function internal_linking($string) {
    $string = str_ireplace(' texttoreplace ', ' <a href="http://www.msn.com">texttoreplace</a> ', $string);
    return $string;
}
add_filter('the_content', 'internal_linking');

?>
It replaces every instance of the word, but I want it to just replace the first instance. How can I fix it?
 


Hmmm...maybe I'm doing it wrong then...

PHP:
<?php

function internal_linking($string) {

    $string = str_ireplace(' texttoreplace ', ' <a href="http://www.msn.com">texttoreplace</a> ', $string);
    return $string;
}

    $i = 0;
    while ($i <= 1){
add_filter('the_content', 'internal_linking');
    $i++;
    }

?>
 
don't ever fuckin use a while loop to make sure something only runs once...

<?
$var = 'abcdef abcdef abcdef';
// pattern, replacement, string, limit
echo preg_replace('/abc/', '123', $var, 1); // outputs '123def abcdef abcdef'
?>
 
You could grab an array and only select the first match to do the replace on. Likely will be [1][0].

edit > disregard....what dchuk said.