0

Consider example:

$mystring = "us100ch121jp23uk12";

I) I want to change value of jp by adding +1 so that makes the string into

us100ch121jp24uk12

suppose if

II) Is there a way to seperate the numeric part and alphabetic part in the above string into:

[us , 100]
[ch,121]
[jp,24]
[us,12]

my code:

$string = "us100ch121jp23uk12";

$search_for = "us";
$pairs = explode("[]", $string); // I dont know the parameters.
foreach ($pairs as $index=>$pair)
{
    $numbers = explode(',',$pair);
    if ($numbers[0] == $search_for){

        $numbers[1] += 1; // 23 + 1 = 24
        $pairs[index] = implode(',',$numbers); //push them back
        break;
    }
}
$new_string = implode('|',$pairs);

using Evan sir's suggestions

$mystring = "us100ch121jp22uk12";

preg_match_all("/([A-z]+)(\d+)/", $mystring, $output);

//echo $output[0][4];


foreach($output[0] as $key=>$value) {
   // echo "[".$value."]";
   echo "[".substr($value, 0, 2).",".substr($value, 2, strlen($value) - 2)."]"."<br>";
}
1
  • 2
    Is it possible to modify your string output? There could be better ways of representing the data - i.e. in an array, with key-value pairs. At least, add delimiters after each country-score pair, such as us100&ch121&jp23&uk12 or the likes (although that is still bad design). Commented Oct 27, 2013 at 16:16

2 Answers 2

2

If you use preg_match_all("/([A-z]+)(\d+)/", $string, $output);, it will return an array to $output that contains three arrays. The first array will be country number strings (eg 'us100'). The second will contain country strings (eg 'us'). The third will contain the numbers (eg '100').

Since the second and third arrays will have matching indexes ($output[1][0] will be 'us' and $output[2][0] will be '100'), you could just cycle through those and do whatever you'd like to them.

Here is more information about using regular expressions in PHP. The site also contains information about regular expressions in general, which are a useful tool for any programmer!

Sign up to request clarification or add additional context in comments.

1 Comment

hello sir, thank you, this is helping me ,but is there a way I can cylce through them so that I can get like the second part of the question?
0

You can do it using regular expressions in PHP. See tutorial: http://w3school.in/w3schools-php-tutorial/php-regular-expression/

Function    Description
ereg_replace()  The ereg_replace() function finds for string specified by pattern and replaces pattern with replacement if found.
eregi_replace() The eregi_replace() function works similar to ereg_replace(), except that the search for pattern in string is not case sensitive.
preg_replace()  The preg_replace() function works similar to ereg_replace(), except that regular expressions can be used in the pattern and replacement input parameters.
preg_match()    The preg_match() function finds string of a pattern and returns true if pattern matches false otherwise.
Expression  Description
[0-9]   It matches any decimal digit from 0 through 9.
[a-z]   It matches any character from lowercase a through lowercase z.
[A-Z]   It matches any character from uppercase A through uppercase Z.
[a-Z]   It matches any character from lowercase a through uppercase Z.
p+  It matches any string containing at least one p.
p*  It matches any string containing zero or more p’s.
p?  It matches any string containing zero or more p’s. This is just an alternative way to use p*.
p{N}    It matches any string containing a sequence of N p’s
p{2,3}  It matches any string containing a sequence of two or three p’s.
p{2, }  It matches any string containing a sequence of at least two p’s.
p$  It matches any string with p at the end of it.
^p  It matches any string with p at the beginning of it.
[^a-zA-Z]   It matches any string not containing any of the characters ranging from a through z and A through Z.
p.p It matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$  It matches any string containing exactly two characters.
<b>(.*)</b> It matches any string enclosed within <b> and </b>.
p(hp)*  It matches any string containing a p followed by zero or more instances of the sequence hp.

you also can use JavaScript: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.