php replace once

Status
Not open for further replies.

ozonew4m

ThisTimeNextYearRodders
Jul 6, 2007
235
3
0
At my desk!
Ok so
ive got some text in the variable $mytext,
ive got a url in the variable $myurl
ive got a list of keyphrases in an array called $mykeyphrases

I only want to place a single link in the text.. doesnt matter which keyphrase but i only want to replace it once and then exit..

can somebody please advise how i do this in php..

cheers guys n gals
 


thanks for you reply but all that does is gives how many times it was replaced...

or am i missing something? :anon.sml:
 
You're right. Too much hemoglobin in my caffiene today.

Try preg_replace. :)

PHP: preg_replace - Manual

you want something like

$newtext = preg_replace( "/$keyword/", "<a href='$link'>$keyword</a>", $oldtext, 1 );

You /probably/ want to play with the back references but I havent done that; it'd be something like

Code:
<?php
$string = $oldtext
$pattern = "/($keyword)/i";
$replacement = "<a href='$link'>${1}</a>";
$newtext =  preg_replace($pattern, $replacement, $string, 1);
?>

Since I don't see a way to specify "replace one of these keywords in this array with a link," I'd probably shuffle the array, use a foreach on the array, do one replacement ( above ), compare the old text to the new text, and if they're the same, then go on through the array; if they're different, then break out of the foreach on the array and continue with what I was doing.
 
  • Like
Reactions: ozonew4m
I'd probably shuffle the array, use a foreach on the array, do one replacement ( above ), compare the old text to the new text, and if they're the same, then go on through the array; if they're different, then break out of the foreach on the array and continue with what I was doing.

Yeah I think that is probably the way to go... gonna be cpu intensive though me thinks

Thanks dude:rasta: +rep
 
Use strpos to find the first occurrence of your keyword. Then do a substr_replace to replace just that keyword, don't forget to specify the length.
If strpos comes back ===false, then do a search for the next keyword in your array.

This is more efficient then doing it via regex, especially if you have a lot of text.
 
  • Like
Reactions: ozonew4m
cheers stanley...

After a bit of searching i found this neat little function that does exactly what i need.. why reinvent the wheel i always say:D

here it is for anybody else who might want it:

Code:
[FONT=Courier New][COLOR=#0000bb]<?php
[/COLOR][COLOR=#007700]function [/COLOR][COLOR=#0000bb]str_replace_count[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$search[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$replace[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$subject[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$times[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]) {
 
    [/COLOR][COLOR=#0000bb]$subject_original[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]$subject[/COLOR][/FONT][FONT=Courier New][COLOR=#007700];
   
    [/COLOR][COLOR=#0000bb]$len[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]strlen[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$search[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);   
    [/COLOR][COLOR=#0000bb]$pos[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]0[/COLOR][/FONT][FONT=Courier New][COLOR=#007700];
    for ([/COLOR][COLOR=#0000bb]$i[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]1[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000bb]$i[/COLOR][COLOR=#007700]<=[/COLOR][COLOR=#0000bb]$times[/COLOR][COLOR=#007700];[/COLOR][COLOR=#0000bb]$i[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]++) {
        [/COLOR][COLOR=#0000bb]$pos[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]strpos[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$subject[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$search[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$pos[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);
       
        if([/COLOR][COLOR=#0000bb]$pos[/COLOR][COLOR=#007700]!==[/COLOR][COLOR=#0000bb]false[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]) {               
            [/COLOR][COLOR=#0000bb]$subject[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]substr[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$subject_original[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]0[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$pos[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);
            [/COLOR][COLOR=#0000bb]$subject[/COLOR][COLOR=#007700].=[/COLOR][COLOR=#0000bb]$replace[/COLOR][/FONT][FONT=Courier New][COLOR=#007700];
            [/COLOR][COLOR=#0000bb]$subject[/COLOR][COLOR=#007700].=[/COLOR][COLOR=#0000bb]substr[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$subject_original[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$pos[/COLOR][COLOR=#007700]+[/COLOR][COLOR=#0000bb]$len[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);
            [/COLOR][COLOR=#0000bb]$subject_original[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#0000bb]$subject[/COLOR][/FONT][FONT=Courier New][COLOR=#007700];
        } else {
            break;
        }
    }
   
    return([/COLOR][COLOR=#0000bb]$subject[/COLOR][/FONT][COLOR=#007700][FONT=Courier New]);
 
}
[/FONT][/COLOR][FONT=Courier New][COLOR=#ff8000]/* Example usage */
[/COLOR][COLOR=#0000bb]$w[/COLOR][COLOR=#007700]=[/COLOR][COLOR=#dd0000]"abracadabra"[/COLOR][/FONT][FONT=Courier New][COLOR=#007700];
print [/COLOR][COLOR=#0000bb]str_replace_count[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"abra"[/COLOR][COLOR=#007700],[/COLOR][COLOR=#dd0000]"----"[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]$w[/COLOR][COLOR=#007700],[/COLOR][COLOR=#0000bb]2[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]);
[/COLOR][COLOR=#0000bb]?>[/COLOR][/FONT]
 
Status
Not open for further replies.