Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
str_replace() .... only once occurence only...
Old 01-03-2005, 05:10 AM str_replace() .... only once occurence only...
johanesw's Avatar
Extreme Talker

Posts: 174
Location: If I told you that, I'm afraid I'd have to kill you!
Trades: 0
Hey guys! I know about this function called str_replace(), which replaces all occurances of the search parameter to the replace parameter. My question is, is there a way to make this only replace the first occurence of the searcy parameter instead of all? Or is there another function to do this?

thxx
__________________

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE
johanesw is offline
Reply With Quote
View Public Profile Visit johanesw's homepage!
 
 
Register now for full access!
Old 01-03-2005, 04:22 PM
Experienced Talker

Posts: 40
Trades: 0
This function should be what you need:

PHP Code:
function replace_first($search$replace$data) {
    
$res strpos($data$search);
    if(
$res === false) {
        return 
$data;
    } else {
        
// There is data to be replaced
        
$left_seg substr($data0strpos($data$search));
        
$right_seg substr($data, (strpos($data$search) + strlen($search)));
        return 
$left_seg $replace $right_seg;
    }

This should help,

Dan
iFuse is offline
Reply With Quote
View Public Profile
 
Old 01-03-2005, 04:58 PM
Novice Talker

Posts: 5
Location: Charleston, SC
Trades: 0
From http://www.sitepoint.com/forums/arch.../t-193846.html:

PHP Code:
function str_replace_once($needle $replace $haystack){
    
// Looks for the first occurence of $needle in $haystack
    // and replaces it with $replace.
    
$pos strpos($haystack$needle);
    if (
$pos === false) {
        
// Nothing found
    
return $haystack;
    }
    return 
substr_replace($haystack$replace$posstrlen($needle));

__________________
---------------------
UnderDog!
underdog is offline
Reply With Quote
View Public Profile
 
Old 06-08-2009, 02:03 PM Re: str_replace() .... only once occurence only...
Junior Talker

Posts: 2
Trades: 0
Quote:
Originally Posted by underdog View Post
From http://www.sitepoint.com/forums/arch.../t-193846.html:

PHP Code:
function str_replace_once($needle $replace $haystack){
    
// Looks for the first occurence of $needle in $haystack
    // and replaces it with $replace.
    
$pos strpos($haystack$needle);
    if (
$pos === false) {
        
// Nothing found
    
return $haystack;
    }
    return 
substr_replace($haystack$replace$posstrlen($needle));

Thank you so much, this is what I needed!
__________________
------ BlogSense Professional ------
Power Commercial Wordpress Plugin

Please login or register to view this content. Registration is FREE
adbox is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to str_replace() .... only once occurence only...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31570 seconds with 12 queries