Solved

retrieve strings from variable in php

Posted on 2012-03-16
6
220 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
MS Dynamics Made Instantly Simpler

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

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

624 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