|
 |
|
 |
10-11-2007, 06:47 AM
|
#1 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
Store String as Hex
Hello,
I currently have two arrays:
- Holding numbers 1-5
- Holding hex values for the 5 numbers
How do I the string for occurrences of the numbers found inside the 1st array, and upon one being found, how do I store the hex values inside the $result variable?
Code:
$string = "32451";
$numbers = array('1','2','3','4','5'); // array1 - stores numbers 1-5
$hex = array('41','42','43','44','45'); // array2 - stores numbers hex values
echo $result;
Example: Using the code above as an example, the result variable should eventually hold the following: "4342444541"
|
|
|
10-11-2007, 09:02 AM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
you could do a loop over the results of explode($string) and use the concatenation operator '.' to add the hex strings onto the end of the $result value.
__________________
Stop intellectual property from infringing on me
|
|
|
10-11-2007, 09:09 AM
|
#3 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
Quote:
Originally Posted by teknomage1
you could do a loop over the results of explode($string) and use the concatenation operator '.' to add the hex strings onto the end of the $result value.
|
Please could you give me an example, I'm new to searching with array.
Cheers 
|
|
|
10-11-2007, 09:32 AM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,446
|
i'm confused, before your example you want to store the hex result in an array, but your example shows you storing the result in a string.
here's how you would store the result in a string.
PHP Code:
<?php $result = ''; $string = "32451"; $numbers = array('1','2','3','4','5'); // array1 - stores numbers 1-5
foreach ($numbers as $each) { if (strstr($string, $each)) { $result .= dechex($each); } } ?>
__________________
Mike
|
|
|
10-11-2007, 10:23 AM
|
#5 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
That's not quite what I'm after!
Can a string be searched for specific characters? - Each time a 6 is found, can "Six" be displayed on the screen, and each time a 5 is found, can "Five" be displayed on the screen.
|
|
|
10-11-2007, 10:36 AM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,446
|
Quote:
|
hat's not quite what I'm after!
|
then you didn't do a very good job at asking the question. could have saved us both some time.
strstr() finds the first occurrence of a string.
preg_match() finds all occurrences in a string and makes available an array of matches.
Quote:
|
Can a string be searched for specific characters? - Each time a 6 is found, can "Six" be displayed on the screen, and each time a 5 is found, can "Five" be displayed on the screen.
|
sure, if you had something setup which related 6 to the string 'Six' and all other possibilities you wanted. i.e. $word_string[6] = 'Six';
__________________
Mike
|
|
|
10-11-2007, 10:39 AM
|
#7 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
Please, could you give me an example?
|
|
|
10-11-2007, 10:42 AM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,446
|
so you can say 'thats not what i was looking for' .. spend at least the time i am spending helping you clarifying what you are getting at. maybe even try to do something and post your code to ask questions about it.
your response question to my initial post is entirely different than your original question. with the speed at which you responded, i don't think you even looked at the links i supplied which would help you search strings.
__________________
Mike
|
|
|
10-11-2007, 10:59 AM
|
#9 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
Oops sorry, I didn't think they were link.
|
|
|
10-11-2007, 12:14 PM
|
#10 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,446
|
Quote:
Originally Posted by Love.Oasis
Please, could you give me an example?
|
of what?
__________________
Mike
|
|
|
10-11-2007, 12:22 PM
|
#11 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
Searching a string for specific characters - Each time a 6 is found, can "Six" be displayed on the screen, and each time a 5 is found, can "Five" be displayed on the screen.
|
|
|
10-12-2007, 06:18 AM
|
#12 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
|
Now you're just repeating yourself, do you realy think that will bring more gratification to the one helping you?
Atleast give us some hint that you've actualy tried anything, or else the interrest for this thread will drop.
|
|
|
10-12-2007, 07:49 AM
|
#13 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
Store String as Hex
OK, I created the following code, that searches a string for occurrences of different numbers stored inside an array. Upon a digit being found, the number in words is stored in the $stringreplaced variable.
Can this code be edited to achieve the end result without using arrays, also can the code just search the string for 2 characters, instead of nine?
I think I'm on the right track, aren't I?
Code:
<?php
$find = array("1","2","3","4","5","6","7","8","9");
$replace = array("one","two","three","four","five","six","seven","eight","nine");
$string = "12166784";
$stringreplaced = str_replace($find, $replace, $string);
echo 'Orig string'.$string.'<br>';
echo $stringreplaced;
?>
|
|
|
10-12-2007, 08:51 AM
|
#14 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,446
|
Hey that's cool. I didn't know str_replace took arrays as arguments.
Quote:
|
Can this code be edited to achieve the end result without using arrays
|
To answer your question though, no. You would at least need to define 1 array which would be the number words keyed by the numbers. To do it that way though you would need to loop through your array to do an individual replacement for each number instead of all of them at once as your code does.
now if you were using only 1 array and replacing characters as you looped through your array, you could define another array or string which would be 'characters to search'. in each loop of the array, you would simply check if that current character existed in the 'characters to search' arrray or string and only do the replacement if it does.
__________________
Mike
|
|
|
10-12-2007, 09:20 AM
|
#15 (permalink)
|
|
banned
Join Date: Oct 2007
Posts: 21
|
So what needs to be changed in the above code, to suit what you mention in the previous post?
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:05 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|