Solved

PHP Regular Expression Pattern Matching

Posted on 2011-03-03
3
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

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.
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to count occurrences of each item in an array.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

623 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