0

I have some php string like below

abc-1987-mp3-songs
xyz-1999-india-mp3-songs
dec-2001-mp3-songs
ryu-2012-freemp3-songs

Now I want these string splited at last found numeric values like below

  abc-1987
  xyz-1999
  dec-2001
  ryu-2012

Please help me that which regex can be used to do this. thanks.

way2project
  • 99
  • 3
  • 8
  • actually i dont have any idea to make my own regex. – way2project Aug 18 '12 at 11:11
  • 1
    split or select....your output is called selection not split – Anirudha Aug 18 '12 at 11:13
  • @way2project: if you want to develop web applications, you're going to have to learn sooner or later: [here](http://weblogtoolscollection.com/regex/regex.php), [or here](http://www.regular-expressions.info/) and of course: google are good places to start. Also know that sites like [this](http://www.functions-online.com) and [this](http://writecodeonline.com/php/) and let's not forget [This one](http://codepad.org/) allow you to practice safely – Elias Van Ootegem Aug 18 '12 at 12:01

3 Answers3

0

Use explode insted of regular expression for example:-

$str="abc-1987-mp3-songs";
$f=explode("-",$str);
echo $final_result=$f[0]."-".$f[1];

or if you want to use reg exp.then

<?php
    $str="abc-1987-mp3-songs";
     echo $f=preg_replace('/[^0-9]/','', $str); 
?>

Above code give you all the numeric digits of your string.

Harshal
  • 3,562
  • 9
  • 36
  • 65
  • that would only work if the numeric values are always at the second position. He says "last numeric" so I assume there can be multiple occurences... – NiklasMM Aug 18 '12 at 11:15
  • no the numeric value can be present at any place like abc-ddd-cerf-1987-mp3-free-songs. I want to spit it at last numeric value found that is like abc-ddd-cerf-1987 – way2project Aug 18 '12 at 11:18
  • but if you want to use reg exp then see my edited code.but using the reg. exp you will also get the another attached digits like mp3(you also got 3). thats why i am recommended you the explode function. – Harshal Aug 18 '12 at 11:26
0

This would match last occurrence of numeric value from given string:

([\w\d-]*-[\d]+)

This is the link: Regex

enter image description here

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
  • would it be preg_match or preg_match_all. and could you give me the full regex pattern please. thanks – way2project Aug 18 '12 at 11:30
  • this is not working dude in the case of aaa-somthing-abc-1987.it is giving only abc-1987 – way2project Aug 18 '12 at 11:32
  • It is working fine but how to use this please help. my string is $string and I used preg_match_all("/([\w\d-]*-[\d]+)/g",$string, &$matches); echo $matches[1]."
    ";
    – way2project Aug 18 '12 at 11:39
0

Ok, I had a look (do take some time to learn regex - but meanwhile):

$split = (preg_split('/(^.*?[0-9]+)\-?[^0-9]+/', 'foo-xyz-1999-india-mp3-songs', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
echo $split[0];//<--- foo-xyz-1999, just like you wanted

Dumps an array with foo-xyz-1999 as first value, which is what you need. If you want to know what every part of the regex does read it here

The only difference is that, though the whole string becomes its own delimiter, there are two delimiters (the first part, always ending on a series of numbers and the rest of the string, that doesn't contain any more digits)

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • hi Elias Van Ootegem. Thank you very very much. Its working fine. Thanks again. – way2project Aug 18 '12 at 12:03
  • Do take the time to read the explanation (the link I provided), and look at my comment to your question for links to both tutorials and sandboxes where you can get to grips with regular expressions – Elias Van Ootegem Aug 18 '12 at 12:05