Jump to content

replacing characters


aramikgh

Recommended Posts

i know that you can do: 

str_replace($replaceWith,$replacethis,$sentence);

lets say i did that 26 times one for each letter of the alphabet.  Can i still do that but once it reaches a charater that is a part of a word that has the "#" symbol in front of it, it wont replace any characters in that string? 

 

so i would want this : " this is a #test"  to be  : "**** ** * #test"

it's not going to be all stars but just using it for this purpose.

 

any comments/ideas?

 

 

thanks!!

Link to comment
https://forums.phpfreaks.com/topic/200093-replacing-characters/
Share on other sites

That words perfect but what if instead of the "*" i wanted each letter to be different like for every "a" it would replace it with a "a1" and be would be "b1" and etc... so $text = "theres #are the abc" would be "t1h1e1r1e1s1 #are t1h1e1 a1b1c1" ??  sorry guess i should have been more clear - i thought there would be a way you can replace each letter with something different except for the letters attached to the word with the #...

 

 

function replace_chars($text) {
    echo '<pre>'.print_r($text, true).'</pre>';
    return str_repeat('*', strlen($text[0]));
}

$text = 'this is a #test';
$result = preg_replace_callback('/(?<!#|[a-z])[a-z]+/i', 'replace_chars', $text);
echo $result;

good questions - i guess i need to think about being more clear, sorry.

 

i would want each letter to have something or even if each letter got replaced with a different set of letters like "a" got replaced with "a1" and "b" got replaced with "493" something like that.. i know i prob would have to write what each letter would be replaced with but how do i avoid a word in the string with a # sign is what im trying to ask.  sorry for the confusion but thanks much for your help!

This function

function replace_chars($text) {
    echo '<pre>'.print_r($text, true).'</pre>';
    return str_repeat('*', strlen($text[0]));
}

runs on every word that doesn't have a # before it

The variable you will need to manipulate is $text[0]

Note that the echo is only there to demonstrate what is matched. You can remove that line. Once you've replaced the letters in the string with the ones you want you can return the string, which will then replace what the old word was. I would probably use str_replace myself, passing an array of letters to replace, and the letters to replace them with

perfect - ill play around with that... thanks!

 

This function

function replace_chars($text) {
    echo '<pre>'.print_r($text, true).'</pre>';
    return str_repeat('*', strlen($text[0]));
}

runs on every word that doesn't have a # before it

The variable you will need to manipulate is $text[0]

Note that the echo is only there to demonstrate what is matched. You can remove that line. Once you've replaced the letters in the string with the ones you want you can return the string, which will then replace what the old word was. I would probably use str_replace myself, passing an array of letters to replace, and the letters to replace them with

K i got what i wanted it to do... thanks!!

but now one more question - im sure it's really easy but i just can't get it... how do i select the words with only the # sign rather then everything but...

 

perfect - ill play around with that... thanks!

 

This function

function replace_chars($text) {
    echo '<pre>'.print_r($text, true).'</pre>';
    return str_repeat('*', strlen($text[0]));
}

runs on every word that doesn't have a # before it

The variable you will need to manipulate is $text[0]

Note that the echo is only there to demonstrate what is matched. You can remove that line. Once you've replaced the letters in the string with the ones you want you can return the string, which will then replace what the old word was. I would probably use str_replace myself, passing an array of letters to replace, and the letters to replace them with

credit goes here james dot randell at hotmail dot co dot uk

http://php.net/manual/en/function.in-array.php

 

<?php 

/** 
* Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values. 
* 
* @param string $string Needle to find 
* @param array $array Haystack to look through 
* @result array Returns the elements that the $string was found in 
*/ 
function find ($string, $array = array ()) { 
foreach ($array as $key => $value) { 
	unset ($array[$key]); 
	if (strpos($value, $string) !== false) { 
		$array[$key] = $value; 
	} 
} 
return $array; 
} 
$sentence = "this is a #test of the function. It #finds more than one word.";
$needle = "#";
$new_a = find($needle, $array = explode(" ",$sentence));
print_r($new_a);

?> 

Would it be possible to do a ...   

$result = preg_replace_callback('/(?<!#|[a-z])[a-z]+/i', 'replace_chars', $text);

that take all the words words with the # at the END and change those strings rather than the #sound being at the begining and not changing those?

 

 

credit goes here james dot randell at hotmail dot co dot uk

http://php.net/manual/en/function.in-array.php

 

<?php 

/** 
* Takes a needle and haystack (just like in_array()) and does a wildcard search on it's values. 
* 
* @param string $string Needle to find 
* @param array $array Haystack to look through 
* @result array Returns the elements that the $string was found in 
*/ 
function find ($string, $array = array ()) { 
foreach ($array as $key => $value) { 
	unset ($array[$key]); 
	if (strpos($value, $string) !== false) { 
		$array[$key] = $value; 
	} 
} 
return $array; 
} 
$sentence = "this is a #test of the function. It #finds more than one word.";
$needle = "#";
$new_a = find($needle, $array = explode(" ",$sentence));
print_r($new_a);

?> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.