Solved

retrieve strings from variable in php

Posted on 2012-03-16
6
217 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 34

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 34

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

831 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