Solved

PHP Regular Expression Pattern Matching

Posted on 2011-03-03
3
397 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

832 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