Jump to content

Recommended Posts

I really can't figure out how to set-up the conditionals in this situation.

 

The function should take the the string, look through it and if it founds a matching word in the string to the words in the array list, replace it. The search should be case-insensitive, but the output of ASCII code should match the original input case on all letters.

 

I'm not sure where to properly put the if statement or how/what I should be using to do the matching. I was figuring preg_match, but I have no idea how to use that with an array.

 

EDIT: Maybe a foreach to iterate over the array, with a preg_match matching any full word (if that's possible); then returning a true or false if a match is found, then wrap the second foreach in an if statement checking to see if the value is true?

 

<?php
error_reporting(E_ALL);

function replace_word($string){
$words = array('Test', 'Test2');

foreach($words as $word){
		if($word==$string){
		$ascii = '';
		$index = 0;
				while($index < strlen($word)) {
				$ascii .= "&#".ord($word[$index]).";";
				$index++;
				}

			echo $ascii . '<br />';
		}
}
return $string;
}

$text="This is a test.";

echo replace_word($text);

?>

 

The expect output should be "This is a (ASCII code for the word "test").".

Alright, I came up with the following; but it's returning a blank page.

 

<?php
error_reporting(E_ALL);

function replace_word($string){
$text="";
$words = array('TEst', 'Test2');

foreach($words as $word){

	if(stristr($string, $word) === TRUE) {

			$ascii = '';
			$index = 0;
					while($index < strlen($word)) {
					$ascii .= "&#".ord($word[$index]).";";
					$index++;
					}

	$text=str_replace($word, $ascii, $string); 
	}
}
return $text;
}

$text="This is a test.";
echo replace_word($text);
?>

Not sure if this will help you, but this is my take on it.

 

<?php
error_reporting(E_ALL);

function replace_word($string){

$words = array('Test', 'test2');	

$ascii = '';

	for ($i=0;$i < count($words);$i++){
		$ascii .= "&#38;#".ord($words[$i]).";";

		$string=str_replace($words[$i], $ascii, $string);
	}

return $string;
}

$text="This is a Test and this is test2 and this is test.";
echo replace_word($text);
?>

I modified it a bit. It works, but not quite how I want it to.

 

<?php
error_reporting(E_ALL);

function replace_word($string){

$words = array('Test', 'test2');	

	for ($i=0;$i < count($words);$i++){
	$ascii = '';
	$index = 0;
			while($index < strlen($words[$i])) {
			$ascii .= "&#".ord($words[$i][$index]).";";
			$index++;
			}

		$string=str_replace($words[$i], $ascii, $string);
	}

return $string;
}

$text="This is a Test and this is test2 and this is test.";
echo replace_word($text);
?>

 

It does replace "Test" and "test2" but I also want it to replace "test". It should be a case insensitive search, but a case sensitive ASCII code generation. So if "Test" and "test" are found in the string it should replace both, but they should be replaced with the ASCII that would generate "Test" and "test" respectively.

 

It should also find TeSt, tEsT, TesT, tEST, etc. and replace with the ASCII to recreate those capitalizations.

 

I could manually put in every single possible capitalization of the words, but that would be a nightmare to make sure I don't miss anything.

 

I had thought the same thing, but that returns "Test" no matter what the initial capitalization. Also test2 is processed as test, so the ASCII code ends up looking like this:

 

This is a &#84;&#101;&#115;&#116; and this is &#84;&#101;&#115;&#116;2 and this is &#84;&#101;&#115;&#116;.

 

(This form software converts ampersands into & #38; so that's what those are in the php box.)

Well I came up with this preg_match...

 

I just have no idea where to put it, or what to run in the If statement.

 

if(preg_match_all("/\b$words[$i]\b/i", $string)){
}

 

That would only work of course, if it's inside the for loop.

Solved with this.

 

function make_ascii($word) {
$arr = str_split($word);
  foreach ( $arr as &$c ) {
    $c = '&#'.ord($c).';';
  }
return implode($arr);
}

function replace_word($string){
$words = array();

    foreach ($words as $word) {
    $string = preg_replace('/\b'.preg_quote($word).'\b/ie', "make_ascii('\\0')", $string);
    }
return $string; 
}  

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.