Member Avatar for LastMitch

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!

Something like this?

list($number, $word) = preg_split('/\^/', $string);
Member Avatar for diafol

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

Member Avatar for LastMitch

@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.

Member Avatar for LastMitch

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?

@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

Member Avatar for LastMitch

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.

Member Avatar for diafol

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.

commented: Thanks for the code & explanation! +12
Member Avatar for LastMitch

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.

Member Avatar for LastMitch

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.