Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Regular Expression Pattern Matching

Posted on 2011-03-03
3
Medium Priority
?
427 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This article discusses how to implement server side field validation and display customized error messages to the client.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

877 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