12 Replies - 988 Views - Last Post: 28 September 2009 - 04:32 AM Rate Topic: -----

#1 dkean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 24-September 09

span of the functions strtr() and str_replace()

Post icon  Posted 24 September 2009 - 02:32 PM

The functions strtr() and str_replace() seem like a wild horse without bridle. attempting to do just one translation in the following string will result in multiple occurrences.

strtr("abcdabcdabcd","a","z") => "zbcdzbcdzbcd".

Is there a way to bridle this wonderful and essential function to doing just one translation which would result in "zbcdabcdabcd"???

In VFP the function allows you to specify the starting occurrence and the number of occurrences to follow StrTranslate("abcdabcdabcd","a","z",2,2) would result in "abcdzbcdzbcd".

Maybe there is another function for that?

TIA

Dennis

Is This A Good Question/Topic? 0
  • +

Replies To: span of the functions strtr() and str_replace()

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: span of the functions strtr() and str_replace()

Posted 24 September 2009 - 02:53 PM

I'm actually not certain about this but I think you would have to write your own custom function for this! :) Have you browsed through all the string-related functions in the PHP documentation?

This post has been edited by Wimpy: 24 September 2009 - 02:53 PM

Was This Post Helpful? 1
  • +
  • -

#3 dkean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 24-September 09

Re: span of the functions strtr() and str_replace()

Posted 24 September 2009 - 03:20 PM

View PostWimpy, on 24 Sep, 2009 - 01:53 PM, said:

I'm actually not certain about this but I think you would have to write your own custom function for this! :) Have you browsed through all the string-related functions in the PHP documentation?



Well, I was not expecting much on this front. I figured I'd have to do it myself, but hope is that member of folly, which compels you to ask, JIC!

Thanx anyway!


Dennis
Was This Post Helpful? 0
  • +
  • -

#4 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: span of the functions strtr() and str_replace()

Posted 25 September 2009 - 08:12 AM

If you haven't already had a chance to look into this it made me curious and i made "something" lol granted it may not be perfect because i have never personally used the function you were originally trying to mock but maybe you could try it out and see how it works for you ... again this is VERY rough and could probably be cleaned up some as well

// testing the function here
echo "started with: abcdabcdabcd<br/>";
echo ".looking for : abcdzbcdzbcd<br/>";
echo ".and we got  : " . StrTranslate("abcdabcdabcd","a","z",2,2);
// end test 


function StrTranslate($contents,$find,$repl,$start=0,$limit=-1) {
	// internal function create a array with keys and 0 values
	function grab_keys($arr) {
		$tmparr = array();
		foreach ($arr as $k=>$v) { $tmparr[$k] = 0; }
		return $tmparr;
	}
	// if limit is default we have no limit so set to string length plus 1
	$limit = $limit == -1 ? strlen($contents) + 1 : $limit;
	// split the string into single chars
	$char = array_filter(preg_split("//",$contents,-1));
	// how many times the char occurs in the string in array format
	$count = array_count_values($char);
	// second array just holds the keys with no values to use later
	$keys = grab_keys($count);
	// thrid array for how many replaces we did
	$times = grab_keys($count);
	/* if the given start occurence is higher than the highesst occurence 
		or doesnt exist at all just return the original string */
	if (!array_key_exists($find,$count) || $count[$find] < $start) { return $contents; }
	// loop and replace as needed
	foreach ($char as $k => $c) {
		if ($keys[$c] < ($start - 1) || $times[$c] >= $limit) { $keys[$c]++; continue; }
		if ($c == $find) { $keys[$c]++; $char[$k] = $repl; $times[$c]++; }
	}
	return implode($char);
}



hope it helps :P ... i couldn't really find any native functions to shorten it but maybe after some research ( or someone smarter than myself ) looking through it could shorten it up a bit

NOTE: this will ONLY work in single word strings ... with some modification it could probably work with sentence and paragraph structures ...

This post has been edited by RPGonzo: 25 September 2009 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 dkean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 24-September 09

Re: span of the functions strtr() and str_replace()

Posted 25 September 2009 - 02:25 PM

View PostRPGonzo, on 25 Sep, 2009 - 07:12 AM, said:

If you haven't already had a chance to look into this it made me curious and i made "something" lol granted it may not be perfect because i have never personally used the function you were originally trying to mock but maybe you could try it out and see how it works for you ... again this is VERY rough and could probably be cleaned up some as well

SNIP



hope it helps :P ... i couldn't really find any native functions to shorten it but maybe after some research ( or someone smarter than myself ) looking through it could shorten it up a bit

NOTE: this will ONLY work in single word strings ... with some modification it could probably work with sentence and paragraph structures ...



Thanx. The idea is great. You made me think and try to do it on my own. I learned that PHP does not quite do what I expect from it much of the time. strpos() seems to return a logical when it cannot find the searched string. Wow! And I have no clue how to trap for that. But here is some code I wrote. It seems to work correctly...

	function strtranslate($string,$find,$replace,$occurrence,$repeat){
		$ptr1 = 1;
		$spos = 0;
		$zlen = 0;
		
		while (strlen(strchr($string,$find))>0){
			$zlen = strlen($find);  //  Get length of the searched string  
			$spos2 = $spos;			//  save previous occurrence position  
			$spos = strpos($string,$find,$spos);  //  Find the position of the first occurence  
			if ($spos2>$spos){
				break;
			}
			$zuk = strlen(strchr(substr($string,$spos),$find));
			if ($ptr1 >= $occurrence){
				$string = substr_replace($string,$replace,$spos,$zlen);
			}
			
			$spos = $spos+$zlen;
			$ptr1 = $ptr1+1;
			if ($ptr1 >= $occurrence+$repeat){
				//echo $string." Done";
				break;
			}
		}
		return $string;
	}
	echo strtranslate("a*1 a*2..a*3,.a*4?..a*5a*6a*7a*8a*9","a*","_",5,15)."<br>";
	echo strtranslate("I smell the rose with my nose near the hose while I watch my wife's pose and notice those morose varicose sections of the hose ready to burst","ose","_",2,14)."<br>";	



And thanx for your version too. Next I will try to do it using the explode/implode functions. PHP does freak me out, but I have to get used to it.

And thank you kindly,
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,598
  • Joined: 08-August 08

Re: span of the functions strtr() and str_replace()

Posted 25 September 2009 - 03:49 PM

- Oops! Misread the question.

This post has been edited by CTphpnwb: 25 September 2009 - 03:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 dkean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 24-September 09

Re: span of the functions strtr() and str_replace()

Posted 25 September 2009 - 04:10 PM

And here is the explode version...

	function strtran($string,$find,$replace,$occurrence,$repeat){
		$strung=" ";
		$arr = explode($find,$string);
		$up2 = count($arr);
		for ($i=1; $i<=$up2-1;$i++){
			$strung = $strung.$arr[$i-1];
			if ($i <= $occurrence-1 || $i>$occurrence+$repeat-1){
				$strung = $strung.$find;
				//echo $i."  strung=".$strung."  array element=".$arr[$i]."<br>";
			}
		}
		$strung = $strung.$arr[$i-1];
		return $strung;
	}
	echo strtran("a*1 a*2..a*3,.a*4?..a*5a*6a*7a*8a*9","a*","_",2,3)."<br>";
	echo strtran("a*1 a*2..","a*","_",1,1);



I think I'm catching on... But it sure takes getting used to these string functions...
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,598
  • Joined: 08-August 08

Re: span of the functions strtr() and str_replace()

Posted 25 September 2009 - 04:39 PM

Here's another method with explode:
<?php
function StrTranslate($str,$search,$replace,$first=1,$last=1)
{
	$mystring = explode($search,$str);
	$return = "";
	if($last< count($mystring))
		$cnt = $last;
	else
		$cnt = count($mystring);
	for($i=0;$i<$first;$i++)
		$return .= $mystring[$i].$search;
	for(;$i<=$cnt;$i++)
	{
		$return .= $mystring[$i].$replace;
	}
	for(;$i<count($mystring)-1;$i++)
		$return .= $mystring[$i].$search;
	$return .= $mystring[$i];
	return $return;
}

$x = "abcdabcdabcdefaghij123a456a";
echo $x."<br>";
echo StrTranslate($x,"a","z",0,3);
echo "<br>";
echo StrTranslate($x,"a","z",2,3);
echo "<br>";
echo StrTranslate($x,"a","z",1,2);
echo "<br>";
echo StrTranslate($x,"a","z",3,4);
echo "<br>";
echo StrTranslate($x,"a","z",4,4);
echo "<br>";
echo StrTranslate($x,"a","z",4,5);
?>

Was This Post Helpful? 0
  • +
  • -

#9 dkean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 24-September 09

Re: span of the functions strtr() and str_replace()

Posted 26 September 2009 - 10:35 AM

I do not understand things like ".=" but I really am thrilled to see another version. It is great to see an alternative approach and the various functions I would never think of, like "$First", "$Last" etc...

Very appreciated,
Was This Post Helpful? 0
  • +
  • -

#10 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: span of the functions strtr() and str_replace()

Posted 26 September 2009 - 10:50 AM

View Postdkean, on 26 Sep, 2009 - 07:35 PM, said:

I do not understand things like ".="


".=" is a string concatenation operator instead of doing:
$hello = "Hello ";
$hello = $hello . "World!";

you can do:
$hello = "Hello ";
$hello .= "World!";

Hope it helps! :)
Was This Post Helpful? 1
  • +
  • -

#11 dkean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 24-September 09

Re: span of the functions strtr() and str_replace()

Posted 27 September 2009 - 08:41 PM

View PostWimpy, on 26 Sep, 2009 - 09:50 AM, said:

View Postdkean, on 26 Sep, 2009 - 07:35 PM, said:

I do not understand things like ".="


".=" is a string concatenation operator instead of doing:
$hello = "Hello ";
$hello = $hello . "World!";

you can do:
$hello = "Hello ";
$hello .= "World!";

Hope it helps! :)



Ah! Now I see what was going on...
Thanx again...
Was This Post Helpful? 0
  • +
  • -

#12 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: span of the functions strtr() and str_replace()

Posted 28 September 2009 - 04:18 AM

No problem, happy to help! :) If you are interested in how each and every operator works you should take a look at the php documentation of those:
http://php.net/manua...e.operators.php, there are plenty of them! :)

View Postdkean, on 28 Sep, 2009 - 05:41 AM, said:

Ah! Now I see what was going on...
Thanx again...

Was This Post Helpful? 0
  • +
  • -

#13 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: span of the functions strtr() and str_replace()

Posted 28 September 2009 - 04:32 AM

I'm sure this can be done in one line with regular expressions. So far it's easy, starting from the beginning and limiting the number of replacements:

echo preg_replace('/a/', 'z', 'abcabcabcabc',1);



I'm sure that an offset could be done by telling the pattern to ignore the first x occurances, but my reg ex skills are a bit lacking :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1