Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-11-2007, 06:47 AM   #1 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
Store String as Hex

Hello,

I currently have two arrays:
  1. Holding numbers 1-5
  2. 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"
Love.Oasis is offline   Reply With Quote
Old 10-11-2007, 09:02 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to 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.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Old 10-11-2007, 09:09 AM   #3 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
Quote:
Originally Posted by teknomage1 View Post
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
Love.Oasis is offline   Reply With Quote
Old 10-11-2007, 09:32 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-11-2007, 10:23 AM   #5 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
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.
Love.Oasis is offline   Reply With Quote
Old 10-11-2007, 10:36 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-11-2007, 10:39 AM   #7 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
Please, could you give me an example?
Love.Oasis is offline   Reply With Quote
Old 10-11-2007, 10:42 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-11-2007, 10:59 AM   #9 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
Oops sorry, I didn't think they were link.
Love.Oasis is offline   Reply With Quote
Old 10-11-2007, 12:14 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
Quote:
Originally Posted by Love.Oasis View Post
Please, could you give me an example?
of what?
__________________
Mike
sde is offline   Reply With Quote
Old 10-11-2007, 12:22 PM   #11 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
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.
Love.Oasis is offline   Reply With Quote
Old 10-12-2007, 06:18 AM   #12 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-12-2007, 07:49 AM   #13 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
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; 
?>
Love.Oasis is offline   Reply With Quote
Old 10-12-2007, 08:51 AM   #14 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 10-12-2007, 09:20 AM   #15 (permalink)
Love.Oasis
banned
 
Join Date: Oct 2007
Posts: 21
Love.Oasis is on a distinguished road
So what needs to be changed in the above code, to suit what you mention in the previous post?
Love.Oasis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM
convert a String to hex sde Java 2 06-03-2004 07:35 AM


All times are GMT -8. The time now is 11:05 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting