Jump to content

[SOLVED] ADVANCED string manipulation (this is tough one, good thinkers welcome)


dsaba

Recommended Posts

i've been trying to figure this out for 2 days, forgive me for being blunt but after 48 hours of trying to figure this out and related things to this problem, I can't afford to meddle in irrelevant things

and I do appreciate the community here in phpfreak, awesome!

 

let me cut to the chase:

basically i want to know how to structure this with string functions, whatever functions neccesary in php, just to get the job done

TERMS: number-thingy = &#5555

          number-unit = &#5555&#5555&#5555

 

old string: &#5555&#1510&#1493&#1512    &#1497&#1510&#1493&#1512    Perfect Creature    &#5555&#1510&#1493&#1512

 

new string: &#1510&#1493&#1512&#1497  &#1510&#1493&#1512&#5555    Perfect Creature    &#1510&#1493&#1512&#5555

 

I want to take the old string, and manipulate it to produce the new string

this string will by dynamic, taken from a database, but things that will always stay the same are:

1. all the number-thingys will always have &# in front of it

2. the number-units will be made up of number-thingys, with the same pattern of &# before each entity

3. there will always be a space between each number-unit and another number-unit

4. there will always be a space between a number-unit and other strings

 

to save you time from bleeding eyes at the monitor to tell the difference between the two strings, I have dont that for you:

1.in the new string each number-unit has been reversed the order of its number-thingys

2.if there are at least 2 number-units next to each other, then the order of the number-units have been reversed

3. the other text strings "perfect creatures" will be left untouched in its relative position next to all the number-units

 

 

....drumroll

-----------------------

can this string manipulation be done???

Link to comment
Share on other sites

because the ad got in the way of formatting I'll repost those two strings

 

old string: &#5555&#1510&#1493&#1512    &#1497&#1510&#1493&#1512    Perfect Creature    &#5555&#1510&#1493&#1512

 

new string: &#1510&#1493&#1512&#1497  &#1510&#1493&#1512&#5555    Perfect Creature    &#1510&#1493&#1512&#5555

 

 

normally there is only ONE space in between number-units and other text strings, but added some extra spaces so you can more clearly see the space between them

Link to comment
Share on other sites

<?php

// old string: &#5555&#1510&#1493&#1512    &#1497&#1510&#1493&#1512    Perfect Creature    &#5555&#1510&#1493&#1512

 

// new string: &#1510&#1493&#1512&#1497  &#1510&#1493&#1512&#5555    Perfect Creature    &#1510&#1493&#1512&#5555

$input = "&#5555&#1510&#1493&#1512 &#1497&#1510&#1493&#1512 Perfect Creature &#5555&#1510&#1493&#1512";

$inpArr = split(" ", $input);

foreach ($inpArr as $val) {

if (ereg("&#", $val)) {

$thingyArr = split("&#", $val);

sort($thingyArr);

foreach ($thingyArr as $val2)

$newString .= "&#" . $val2;

 

$newString .= " ";

}else {

$newString .= $val . " ";

}

}

 

$newString = str_replace("&#&#", "&#", $newString);

// &#1493&#1510&#1512&#5555 &#1493&#1497&#1510&#1512 PerfectCreature &#1493&#1510&#1512&#5555

print $newString;

?>

 

It did not print out exactly like the output, but yea, Is that what you were looking for?

 

 

NOTE used quote due to code tags messing up my code.

Link to comment
Share on other sites

if you want to compare strings look at these (i mistyped the last ones)

old string: &#5555&#1510&#1493&#1512    &#1497&#1510&#1493&#1512    Perfect Creature    &#5555&#1510&#1493&#1512

new string: &#1512&#1493&#1510&#1497  &#1512&#1493&#1510&#5555    Perfect Creature    &#1512&#1493&#1510&#5555

 

if this helps anyone here is some more information on the rules of the string manipulation:

1. number-thingys will always 6 characters long, start with &# and end with a number

2. number-units will always have at least 2 of "&", start with & and end with a number

 

this is my brainstorm:

1. split the string by spaces

2. reverse the order of the number-units if at least 2 of them are near each other

3. reverse the order of the number-thingys inside the number units

4. keep the other text strings where they are

 

 

Link to comment
Share on other sites

EDIT This should work exactly how you want it!

 

<?php

// old string: &#5555&#1510&#1493&#1512    &#1497&#1510&#1493&#1512    Perfect Creature    &#5555&#1510&#1493&#1512

// new string: &#1512&#1493&#1510&#1497  &#1512&#1493&#1510&#5555    Perfect Creature    &#1512&#1493&#1510&#5555

 

$input = "&#5555&#1510&#1493&#1512 &#1497&#1510&#1493&#1512 Perfect Creature &#5555&#1510&#1493&#1512";

$inpArr = split(" ", $input);

 

for ($i=0;$i<count($inpArr);$i++) {

if (ereg("&#", $inpArr[$i])) {

$oldi = $i;

if (isset($inpArr[($i+1)]) && ereg("&#", $inpArr[($i+1)])) {

$thingy2Arr = split("&#", $inpArr[($i+1)]);

array_shift($thingy2Arr);

krsort($thingy2Arr);

foreach ($thingy2Arr as $val2)

$newString .= "&#" . $val2;

 

$newString .= " ";

$i++;

}

 

$thingyArr = split("&#", $inpArr[$oldi]);

array_shift($thingyArr);

krsort($thingyArr);

foreach ($thingyArr as $val2)

$newString .= "&#" . $val2;

 

$newString .= " ";

}else {

$newString .= $inpArr[$i] . " ";

}

}

 

//&#1512&#1493&#1510&#1497 &#1512&#1493&#1510&#5555 Perfect Creature &#1512&#1493&#1510&#5555

print $newString;

?>

 

EDIT:

 

Give me a few, I think I have it now. I will re-post the code on this reply.

Link to comment
Share on other sites

thanks a lot frost! I appreciate your efforts, I haven't had enough time to analyze your code so much yet, to tell you what needs changing, etc...

 

however i did try them out here are the results:

 

//what I want

&#1512&#1493&#1510&#1497 &#1512&#1493&#1510&#5555 Perfect Creature &#1512&#1493&#1510&#5555

//what came out with 1st script

&#1493&#1510&#1512&#5555 &#1493&#1497&#1510&#1512 Perfect Creature &#1493&#1510&#1512&#5555

//what came out with 2nd script

&#1512&#1493&#1510&#1497&# &#1512&#1493&#1510&#5555&# Perfect Creature &#1512&#1493&#1510&#5555&#

 

everything looks good except for the extra # at the end of the number-unit

I suppose i can just split the new string again, and remove # from the end of each number-unit

Link to comment
Share on other sites

<?php
// old string: ᖳצור     יצור    Perfect Creature     ᖳצור
// new string: רוצי   רוצᖳ     Perfect Creature    רוצᖳ

$input = "ᖳצור יצור Perfect Creature ᖳצור";
$inpArr = split(" ", $input);

for ($i=0;$i<count($inpArr);$i++) {
if (ereg("", $inpArr[$i])) {
	$oldi = $i;
	if (isset($inpArr[($i+1)]) && ereg("", $inpArr[($i+1)])) {
		$thingy2Arr = split("", $inpArr[($i+1)]);
		array_shift($thingy2Arr);
		krsort($thingy2Arr);
		foreach ($thingy2Arr as $val2) 
			$newString .= "" . $val2;

		$newString .= " ";
		$i++;
	}

	$thingyArr = split("", $inpArr[$oldi]);
	array_shift($thingyArr);
	krsort($thingyArr);
	foreach ($thingyArr as $val2) 
		$newString .= "" . $val2;

	$newString .= " ";		
}else {
	$newString .= $inpArr[$i] . " ";
}
}

//רוצי רוצᖳ Perfect Creature רוצᖳ 
print $newString;
?>

 

That should produce the wanted results.

Link to comment
Share on other sites

MOST RECENT POST IGNORE THE OTHER STUFF AND LOOK AT THIS ONE!

AWESOME JOB!!!! FROST!!!!!

thank you! :) :) :)

(there are complications in the way in which the script acts when fed different types of strings than the one I used as an example, so i want to try another method that failed me before, hence the question)

 

 

i have another question related to my main goal of doing this string manipulation

-------------------------------------------------------------------------------

there is an easier way to do this, but I had no success in doing it my way, i'll tell you what i think the solution was, and what I tried, and how it didn't work

 

&#1497&#1510&#1493&#1512     &#1502&#1493&#1513&#1500&#1501     Perfect Creature

 

this here was originally hebrew and english, (the english remains intact)(while each hebrew word has become a number-unit)

 

I want to write this string onto an image with the imagettftext(); function

I have successfully done that when the string looks like this:

יצור מושלם Perfect Creature

 

as you can see now the string has become:

&#1497&#1510&#1493&#1512     &#1502&#1493&#1513&#1500&#1501     Perfect Creature

 

so my I'm thinking i need to "decode" the number-units back into their hebrew characters, AS thats the only way the imagettftext() function will take it

 

so i research this encoding format and I find this out:

Name HEBREW LETTER FINAL MEM

Block Hebrew

Category Letter, Other [Lo]

Combine 0

BIDI Right-to-Left [R]

Mirror N

Version Unicode 1.1.0 (June, 1993)

Encodings

HTML Entity (decimal) &#1501;

HTML Entity (hex) &#x5dd;

How to type in Microsoft Windows Alt +05DD

 

UTF-8 (hex) 0xD7 0x9D (d79d)

UTF-8 (binary) 11010111:10011101

UTF-16 (hex) 0x05DD (05dd)

UTF-16 (decimal) 1,501 

UTF-32 (hex) 0x000005DD (05dd)

UTF-32 (decimal) 1,501

C/C++/Java source code "\u05DD"

Python source code u"\u05DD"

 

 

so now I know that the number-thingy is in fact encoded in html entity (decimal)

so now I try to do this:

html_entity_decode($string);  - does not work

html_specialchars_decode($string) - does not work

 

 

so my question is how do I successfully convert the encrypted hebrew words back into their utf-8 hebrew characters which they were encoded with in the first place

 

if you're going suggest that an browser will interprete and decode these characters for me and then display them, I am well aware of that, however it is not in the browser where I need the hebrew characters to display

 

-IT is in the actual php script, because I need to feed the imagettftext() function with it, and it is not a browser and does not interpret encrypted hebrew characters

 

-ANY THOUGHTS??? ----------thanks a bunch

Link to comment
Share on other sites

THIS IS THE MOST RECENT RECENT POST, IGNORE THE OTHER POSTS AND READ THIS ONE, YOU MIGHT WANT TO READ THE FIRST POST WHERE I DEFINE "TERMS" WHAT NUMBER-THINGYS AND NUMBER-UNITS MEAN AS I WILL BE USING THOSE TERMS IN MY QUESTION - THANK YOU

 

ok i unchecked this topic as "solved" because it wasn't really solved, and I need help with the same code but a little different this time

 

old string: יצור מושלם Perfect Creature

new string:   מושלם יצור  Perfect Creature

 

the number-thingys/number-units are pointless, or obsolete for what i'm trying to do, so now I want to customize frost's code to manipulate that string above

 

here's is what I have so far on changing his script, I don't understand all of frost's code as thats where my question comes in

 

 

in frost's code he first identifies "number-units" and determines if there are at least 2 number-units next to each other and then reverses the order of those number units, leaving the other text strings exactly where they are

 

basically I want to do the same thing with this string except instead of reversing the order of number-units i want to reverse the order of "hebrew words"

 

i have come up with this to identify hebrew words, just like before we identified number-units as a string that has at least two of "&" and starts with & and ends with a number

 

to identify hebrew words in a string this is what I do:

              $matches = preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $input);

I use that above regex, it is a list of the hebrew alphabet, (there are no upper/lower cases in the hebrew alphabet, its all one case)

 

before he was using this:

if (ereg("&#", $inpArr[$i])) {

 

another thing I want to change in frost's script is where he reverses the order of the "number-thingys" inside the number-units, that I no longer want to do

 

so for example in this string:

יצור מושלם Perfect Creature

 

after i'm done reversing the order of hebrew words if there are at least 2 words next to each other, I don't want to reverse the order of the letters inside the word

new string: מושלם יצור  Perfect Creature

 

as you can see the order of the hebrew letters inside the words are left intact

 

and thats pretty much what i want to change in frost's script, how would I go about doing that?

here is the most current version of frost's script:

 

<?php
function ready_hebrew($input)														{
$inpArr = split(" ", $input);

for ($i=0;$i<count($inpArr);$i++) {
if (ereg("&#", $inpArr[$i])) {
	$oldi = $i;
	if (isset($inpArr[($i+1)]) && ereg("&#", $inpArr[($i+1)])) {
		$thingy2Arr = split("&#", $inpArr[($i+1)]);
		array_shift($thingy2Arr);
		krsort($thingy2Arr);
		foreach ($thingy2Arr as $val2) 
			$newString .= "&#" . $val2;

		$newString .= " ";
		$i++;
	}

	$thingyArr = split("&#", $inpArr[$oldi]);
	array_shift($thingyArr);
	krsort($thingyArr);
	foreach ($thingyArr as $val2) 
		$newString .= "&#" . $val2;

	$newString .= " ";		
}else {
	$newString .= $inpArr[$i] . " ";
}
}

//&#1512&#1493&#1510&#1497 &#1512&#1493&#1510&#5555 Perfect Creature &#1512&#1493&#1510&#5555 
global $newString;
												  //end hebrew_ready function
																			}
//-----------------------------------HEBREW READY FUNCTION------------------------------------------------------------	
?>

 

Link to comment
Share on other sites

EDIT: changed to quote so it displays properly.

 

This should produce the desired results.

 

<?php

function ready_hebrew($input) {

global $newString;

$inpArr = split(" ", $input);

 

for ($i=0;$i<count($inpArr);$i++) {

if (preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $inpArr[$i])) {

$oldi = $i;

if (isset($inpArr[($i+1)]) && preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $inpArr[($i+1)])) {

/* $thingy2Arr = split("&#", $inpArr[($i+1)]);

array_shift($thingy2Arr);

krsort($thingy2Arr);

foreach ($thingy2Arr as $val2)

$newString .= "&#" . $val2;

*/

// the above is no longer needed unless we want to reverse the chars.

 

$newString .= $inpArr[($i+1)] . " ";

$i++;

}

 

/*$thingyArr = split("&#", $inpArr[$oldi]);

array_shift($thingyArr);

krsort($thingyArr);

foreach ($thingyArr as $val2)

$newString .= "&#" . $val2;

*/

// Again the above is no longer needed due to no more reversing the words.

$newString .= $inpArr[$oldi] . " ";

}else {

$newString .= $inpArr[$i] . " ";

}

}

 

//end hebrew_ready function

}

 

$input = 'יצור מושלם Perfect Creature';

ready_hebrew($input);

print $newString;

//-----------------------------------HEBREW READY FUNCTION------------------------------------------------------------

?>

Link to comment
Share on other sites

frost run this test and follow these steps:

1. paste this code into notepad

2. save it under "UTF-8"

3. run the script in your browser

 

code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>frost test</title>

</head>

<body>

<?php

//---------------------------------HEBREW_READY---------------------------------------------------------------

function ready_hebrew($input) {

   global $newString;

   $inpArr = split(" ", $input);

 

   for ($i=0;$i<count($inpArr);$i++) {

      if (preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $inpArr[$i])) {

         $oldi = $i;

         if (isset($inpArr[($i+1)]) && preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $inpArr[($i+1)])) {

/*            $thingy2Arr = split("&#", $inpArr[($i+1)]);

            array_shift($thingy2Arr);

            krsort($thingy2Arr);

            foreach ($thingy2Arr as $val2)

               $newString .= "&#" . $val2;

*/

            // the above is no longer needed unless we want to reverse the chars.

           

            $newString .= $inpArr[($i+1)] . " ";

            $i++;

         }

        

         /*$thingyArr = split("&#", $inpArr[$oldi]);

         array_shift($thingyArr);

         krsort($thingyArr);

         foreach ($thingyArr as $val2)

            $newString .= "&#" . $val2;

         */

         // Again the above is no longer needed due to no more reversing the words.

         $newString .= $inpArr[$oldi] . " ";     

      }else {

         $newString .= $inpArr[$i] . " ";

      }

   }

 

//end hebrew_ready function

}

//-----------------------------------HEBREW READY FUNCTION------------------------------------------------------------

 

 

 

 

$string = "ואני גר בה ישראל yossi שלום השם שלי hello וואלא";

ready_hebrew($string); //outputs $newString

 

echo '$newString: '.$newString;

echo '<br><img src="http://p2mhunt.freehostbr.com/images/frost.png" />';

 

 

 

 

 

?>

</body>

</html>

 

 

you should get this result:

http://p2mcity.freehostia.com/frost.php

frost.png

 

 

and you should be able to read the hebrew, if not right click and choose utf-8 encoding

 

this pic explains what should happen and what actually does happen, keep in mind the goal is to get the string to "what the script outputs" and not "what the browser outputs"

 

only reason i put what the browser outputs is because if u test it by echoing to the browser the browser will do additional reversing on teh hebrew words, and thus not truly show u what the script outputs

 

the only way to see what it truly outputs is to write the string to an image where no additional reversing will happen, i have done that for you and know what the result should be and what your script truly outputs

 

if you compare "what your script outputs" on success/unsuccessful you will notice that the order of the hebrew words are slightly off, the goal is to get it to look like "what your script outputs" on success

Link to comment
Share on other sites

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

 

<?php

function ready_hebrew($input) {

global $newString;

$inpArr = split(" ", $input);

 

for ($i=0;$i<count($inpArr);$i++) {

$wordArr = array(); // reset array

if (preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $inpArr[$i])) {

$wordArr[0] = $inpArr[$i];

$x=1;

while (isset($inpArr[($i+1)]) && preg_match("/[קראטוןםפשדגכעיחלךףזסבהנמצתץ]/", $inpArr[($i+1)])) {

$i++;

$wordArr[$x++] = $inpArr[$i];

}

 

$num = $x;

for ($x=$num;$x > -1; $x--) {

$newString .= $wordArr[$x] . " ";

}

}else {

$newString .= $inpArr[$i] . " ";

}

}

 

//end hebrew_ready function

}

 

$input = "ואני גר בה ישראל yossi שלום השם שלי hello וואלא";

ready_hebrew($input);

print $newString;

//-----------------------------------HEBREW READY FUNCTION------------------------------------------------------------

?>

 

Hopefully that is the ticket.

Link to comment
Share on other sites

AMAZING!!! 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

 

 

GOLD STARS FOR YOU FROST!!!!

 

:):):):):):):):):)

Link to comment
Share on other sites

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.