Solved

PHP Regular Expression Pattern Matching

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
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
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

749 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