Solved

retrieve strings from variable in php

Posted on 2012-03-16
6
219 Views
Last Modified: 2012-06-27
Hi,

I have a variable $var1 and it will always contain a string like "UK1234" or say "EWP123456". I basically need to get into $var2 the text prefix and into $var3, the rest of the numeric string. Problem is that the text prefix may be 2 characters or it may be 3 characters, so I can't use something like,

$var2 = substr($var1,0,2);

If it helps, the text prefix in he string can only be one of the following.....


UK
NL
IR
ETS
EWP
GM
PL


So, some data examples of what I need are as follows.

$var1 = "EWP123456"
$var2 = "EWP"
$var3 = "123456"

$var1 = "UK1234"
$var2 = "UK"
$var3 = "1234"

Thanks.
0
Comment
Question by:RupertA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 37729510
Use preg_match:

if(preg_match("/([A-Z]+)([0-9]+)/",$var1,$matches))
{
  $textprefix = $matches[1];
  $digits = $matches[2];
}
0
 

Author Comment

by:RupertA
ID: 37729661
That does work for my purposes gr8gonzo. Thanks!

Will leavet the thread open for a while in case anybody else wanted to comment on the thread. But I am happy with that as a solution.
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 37729787
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:gr8gonzo
ID: 37729857
@Captain - not sure how that applies here or why the author wouldn't just use preg_replace() to do the same thing?
0
 
LVL 27

Expert Comment

by:CaptainCyril
ID: 37729928
@gr8gonzo - It's just another way of doing it. preg_replace() is excellent and does the job required.
0
 

Author Comment

by:RupertA
ID: 37730496
Thanks a lot for the help! I went the great gonzo's ;)
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question