Solved

retrieve strings from variable in php

Posted on 2012-03-16
6
214 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now