Solved

retrieve strings from variable in php

Posted on 2012-03-16
6
216 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 List with Ratings 6 32
Regular Expression Calculator Tester 2 53
Angular - "nest" service calls? 18 22
Recordset containing single quotes 8 21
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

15 Experts available now in Live!

Get 1:1 Help Now