php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #5919 stri_replace() to compliment str_replace()
Submitted: 2000-08-02 12:33 UTC Modified: 2003-01-29 23:02 UTC
Votes:162
Avg. Score:4.4 ± 0.8
Reproduced:141 of 145 (97.2%)
Same Version:83 (58.9%)
Same OS:37 (26.2%)
From: juhl at eisenstein dot dk Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.3.0 OS: Slackware Linux 7.0
Private report: No CVE-ID: None
 [2000-08-02 12:33 UTC] juhl at eisenstein dot dk
It would be extremely nice to have a case insensitive version of str_replace() so that you don't have to use the much slower eregi_replace() when you need to replace strings of mixed case.
I believe a logical name for such a function would be stri_replace()

If there are no current plans to implement such a feature, would a patch that implements it be accepted?


Best regards,
Jesper Juhl
juhl@eisenstein.dk

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-09-16 01:18 UTC] derick@php.net
A patch that implements this feature will probably accepted. That is when it's nicely written of course.
 [2000-09-23 18:40 UTC] juhl@php.net
> A patch that implements this feature will probably accepted

In that case I'll start writing a patch :-)

 [2000-11-21 04:04 UTC] sniper@php.net
Where's the patch? =)
 [2001-05-07 10:57 UTC] derick@php.net
Closing because of no feedback
 [2002-01-13 17:45 UTC] philip at cornado dot com
Please submit this patch :)
 [2002-05-02 18:19 UTC] philip@php.net
I marked similar feature requests as duplicates (bugs #10352 and #14215) and am changing the status of this to open.  This feature request comes up fairly often, it's still open :)
 [2002-05-21 20:09 UTC] gearond at cvc dot net
Russians call the '@', little dog, 'sabachkee'. Anyway, I like the idea of stri_replace because the only alternative is a slower, and more complex usage of [eregi|pregi]_replace. And if one is replacing a lot of symbols, (like I am), and not just strings, then extensive knowledge of regex is required , (which I don't have yet), to replace all the symbols. I'm trying to escape all the key words and key symbols in Oracle's applications. Works great with str_replace, **IF** I want to change the whole string to upper or lower case, or use a function of my own. Otherwise, it sucks. I am implementing one of my own for now though, (actually copying the non recursive one under 'str_replace()'. To see an example of it, see: http://www.qweeka.com/db_tools.php after memorial day.
 [2002-05-25 18:26 UTC] webmaster at tsn dot dk
I think it's a shame that two years after this feature was requested it has not been implemented... 

Lots of people are missing this feature and therefore it would be a great addition to PHP... If anyone knows how to make a patch for this, please do so... I am sure it would be appreciated by many.
 [2002-05-25 18:42 UTC] rasmus@php.net
Nothing is stopping you from submitting a patch that implements this.  People generally add the features they need.  Apparently nobody capable of writing this feature has ever needed it.  (That might tell you something)
 [2002-10-30 04:49 UTC] fergus at serif dot net
In reply to rasmus@php.net, perhaps the reason people capable of adding this feature don't is because they have reached the level where they are able to understand ereg_replace sufficiently. Unfortunately not all developers can. It's elitist to argue that novices should be denied an obviously useful function until they have mastered the language. Not to mention that this function would speed up the development time for a lot of developers.
 [2002-10-30 05:55 UTC] derick@php.net
Well, if you really need it you can always hire a competent developer to do it, it's Open Source after all...
 [2002-11-01 12:59 UTC] php at bkjproductions dot com
What about just adding a parameter to the existing function, rather than making a whole new function? You could just put "sensitive" or "insensitive" (maybe "callous?") or something on the end:
str_replace($a, $b, $c, "sensitive")
I think that some other languages (maybe the ill-fated Oracle Media Objects) have that.

Another parameter could be "match" so that if
$a="street" 
$b= "avenue"
$c= "1313 Mockingbird Street" 
the result would be
"1313 Mockingbird Avenue"
the idea being that the search routine realized that 
	Street 
was capitalized, so therefore 
	Avenue 
should match the case.
 [2002-12-28 18:38 UTC] mike dot h at iwmi dot com
It would be extreamly great if some one....perhaps some one who volunteered might submit a patch to impliment this, 137 votes already, why can't we get some type of a feature voting system, to vote for the things that are the most important to those who don't have the skills, or perhaps time, or money to do these themselves.
 [2003-01-29 23:02 UTC] pollita@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.

Only took 29 months to do it too!
 [2003-03-18 04:18 UTC] cvr at solcon dot org
This feature would be very helpfull!!
 [2003-03-21 03:20 UTC] ruud at phpjunk dot nl
Damn.. i need this function..

i've fixed it with this:

function highlight_search_string ($needle, $haystack, $start, $end)
	{
		$parts = explode( strtolower($needle), strtolower($haystack));
		$pos = 0;

		foreach($parts as $key => $part)
		{
			$parts[ $key ] = substr($haystack, $pos, strlen($part));
			$pos += strlen($part);

			$parts[ $key ] .= $start.substr($haystack, $pos, strlen($needle)).$end;
			$pos += strlen($needle);
		}
		return(join('', $parts));
	}

but that's not everything.. i hope this feat will be made in the new version of php. .. tx
 [2003-11-17 19:43 UTC] php_pp at yahoo dot com
Try this ...

function stri_replace($search, $subject, $replace=false){
    $textn=$subject;
    $texti=strtolower($subject);
    $wordi=strtolower($search);
    $recd=array();
    $len=strlen($wordi);
    while(is_integer($pos=strpos($texti,$wordi))){
        $rec["str"]=substr($textn,$pos,$len);
        $rec["pre"]=substr($textn,0,$pos);
        $recd[]=$rec;
        $cut=$pos+$len;
        $texti=substr($texti,$cut);
        $textn=substr($textn,$cut);
    }

    for($i=0;$i<count($recd);$i++){
        if(!$replace) $replace="<b>".$recd[$i]["str"]."</b>";
        $ntext=$ntext.$recd[$i]["pre"].$replace;
    }
    $ntext.=$textn;
    return $ntext;
}
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 17:01:24 2019 UTC