Solved

PHP Regular Expression Pattern Matching

Posted on 2011-03-03
3
372 Views
Last Modified: 2012-06-21
I have various dynamic strings in PHP that I am trying to split

$string = "Man Utd v Liverpool - Premiership";
$string = "World Championships - Oslo - Ski Jumping";
$string = "Regional Heats - Sussex - UK - Tiddliwinks";

I need to split the strings based on the LAST " - " to read

$string = "Man Utd v Liverpool";
$string = "World Championships - Oslo";
$string = "Regional Heats - Sussex - UK";

How can I do this? My Regular Expression Pattern skils are very basic.

Thanks

Mike
0
Comment
Question by:hungoveragain
3 Comments
 
LVL 4

Expert Comment

by:florjan
ID: 35028964
explode might be the command you seek.
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 250 total points
ID: 35029007
Try this

<?php

$string = array();

$string[] = "Man Utd v Liverpool - Premiership";
$string[] = "World Championships - Oslo - Ski Jumping";
$string[] = "Regional Heats - Sussex - UK - Tiddliwinks";
$string[] = "Man Utd v Liverpool";
$string[] = "World Championships - Oslo";
$string[] = "Regional Heats - Sussex - UK";

foreach( $string as $aString ) {

     preg_match('#^(.+?)-([^-]+)$#', $aString, $match );


     // show results
     //
     echo "Original: '$aString'<br/>";
     echo "<pre>";
     print_r($match);
     echo "</pre>";
     echo "<br/>";

}

Open in new window


Which gives

Original: 'Man Utd v Liverpool - Premiership'

Array
(
    [0] => Man Utd v Liverpool - Premiership
    [1] => Man Utd v Liverpool
    [2] =>  Premiership
)


Original: 'World Championships - Oslo - Ski Jumping'

Array
(
    [0] => World Championships - Oslo - Ski Jumping
    [1] => World Championships - Oslo
    [2] =>  Ski Jumping
)


Original: 'Regional Heats - Sussex - UK - Tiddliwinks'

Array
(
    [0] => Regional Heats - Sussex - UK - Tiddliwinks
    [1] => Regional Heats - Sussex - UK
    [2] =>  Tiddliwinks
)


Original: 'Man Utd v Liverpool'

Array
(
)


Original: 'World Championships - Oslo'

Array
(
    [0] => World Championships - Oslo
    [1] => World Championships
    [2] =>  Oslo
)


Original: 'Regional Heats - Sussex - UK'

Array
(
    [0] => Regional Heats - Sussex - UK
    [1] => Regional Heats - Sussex
    [2] =>  UK
)


0
 

Author Closing Comment

by:hungoveragain
ID: 35035213
Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

757 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

23 Experts available now in Live!

Get 1:1 Help Now