1

Hi

I'm not sure what to used to split numbers and letter from words.

So far I try preg_split() function and also mb_split() function both gave me No ending delimiter ^

There is ^ (caret).

For example my data is from my database and it's not an array and it nothing to do with any password or security.

It's an example:

I echo out:

<?php echo $numbers; ?>^<?php echo $words; ?>

Then the result is

 123^ABC

How do I separate 123^ABC and echo out $word & $number individually on a different page.

I am using list() function:

list($number, $word)=preg_split('^', $word)

echo '$number'; // 123
echo '$word'; // ABC 

list($number, $word)=mb_split('^', $number)

echo '$number'; // 123
echo '$word'; // ABC 

Any Suggestions and explanations will help. I appreciate it. Thanks!

4
Contributors
9
Replies
47
Views
4 Years
Discussion Span
Last Post by LastMitch
2

Something like this?

list($number, $word) = preg_split('/\^/', $string);

Edited by pixelsoul

2

As pixelsoul shows, you must escape the caret to make it literal, as it has a special meaning in regex.

0

@pixelsoul

Thanks for the reply and example. I will take a look at it and test it out.

@diafol

Thanks for the reply and explanation. I will look into that.

0

As pixelsoul shows, you must escape the caret to make it literal, as it has a special meaning in regex.

I think change my mind. I didn't take notice about the caret are special characters in regex.

What is the best symbol to separate the letters(words) and numbers?

2

@lastMitch again take care....if your string doesn't contain your delimiter,you will get error notice like i explained yesterday for list.use array_pad() to make sure array size is not less that two.

As for symbol that you want to use as a delimiter,i would suggest that before deciding the symbol make sure it will not be in your data part.That data must be validation for special characters.your number part must only contain number and word part only word.
take a look at this example.

$string="665^ftt^yyy";

Now it will be a problem.so make sure whatever delimiter you use,it must not be within data

0

As for symbol that you want to use as a delimiter,i would suggest that before deciding the symbol make sure it will not be in your data part.

Thanks for the reply and explanation.

My intention was something simple for me to remember a delimiter that can separate a letters and number.

The array_pad() function works fine. I think because it only has 1 digit in each column.

Yesterday, the issue I had was the Undefined offset: 1 notice.

But for this table each column (there's only 2 column) has 3 letters(1 column) and 3 numbers(1 column).

Does it work with explode() function to split those 3 letters and 3 numbers or the preg_match() function does it easier?

I don't used these function much as you can see why I don't understand these notice this is the first time.

Edited by LastMitch: grammer

2

explode is easier than preg_split and it will be quicker (imo).

$str = '123^pig';

function checkNW($str,$sep='^')
{
    return (strpos($str,$sep) !== false) ? explode($sep,$str) : false;
}
echo (list($num,$word) = checkNW($str)) ? "NUM: $num  WORD: $word" : "ERROR";

Of course, preg functions have the advantage of easily being able to check groups. * The above function will just return the first two exploded items - no checks for the number of separators in the original string. Neither does it check the type (numeric/string) or length.

Edited by diafol

Votes + Comments
Thanks for the code & explanation!
0

explode is easier than preg_split and it will be quicker (imo).

Thanks for your opinion and example. I will test it out and play around with the code you provide to get a idea how it works.

0

Of course, preg functions have the advantage of easily being able to check groups. * The above function will just return the first two exploded items - no checks for the number of separators in the original string. Neither does it check the type (numeric/string) or length.

Thanks diafol. The small code snippet works!

I finally got it working meaning the column are separating into $number and $word.

I got a better understanding how the code works now. Thanks for the code & explanation.

Edited by LastMitch: grammer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.