Guest Posted July 23, 2012 Share Posted July 23, 2012 $tshirt is the name of articles followed by a code "A123456789". The code is random numbers but always begin by "-A" I'm trying to get the name of the article WITHOUT the number code Here's the code i am currently using: $tshirt = "Name-of-the-tshirt-A123456789"; $categorie = explode("-A", $tshirt); $categorie = $categorie[0]; echo $categorie; Problem with this code is that it is bugged if the article looks like this : $tshirt = "tshirt-anime-A123456789"; Then the result will be "tshirt" when it's supposed to be "tshirt-anime" How can i add some sort of wildcard to be sure that "-A" is followed by a number ? Thanks Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/ Share on other sites More sharing options...
xyph Posted July 23, 2012 Share Posted July 23, 2012 Well, you should be using $categorie[1] Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/#findComment-1363875 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2012 Share Posted July 23, 2012 You want the description without the number, right? Assuming all the numbers are always at the end, explode on the hyphen, array_pop the last element off the array, then implode the array with a hyphen as the glue. Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/#findComment-1363878 Share on other sites More sharing options...
xyph Posted July 23, 2012 Share Posted July 23, 2012 Oops, my bad. Misread. Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/#findComment-1363881 Share on other sites More sharing options...
Guest Posted July 24, 2012 Share Posted July 24, 2012 You want the description without the number, right? Assuming all the numbers are always at the end, explode on the hyphen, array_pop the last element off the array, then implode the array with a hyphen as the glue. Thanks a lot ! Did exactly what i wanted Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/#findComment-1363887 Share on other sites More sharing options...
jazzman1 Posted July 24, 2012 Share Posted July 24, 2012 In this case, I could use regEx, take a look at suggestions: // match any characters in the string $tshirt = "Tes-t, ... Testzaz ^5 &&, Name-of-athe-tshirt-A123456759 Test after"; $pattern = '/[A-Za-z-\S]+(?=\-A\d)/'; if(preg_match($pattern, $tshirt, $matches)){ echo '<pre>'.print_r($matches, true).'</pre>'; } // match any characters in the string $tshirt = "Test, ... Testzaz ^5 &%, Name-of-*&%athe-Atshirt-A123456789 Test after"; $pattern = '/[.\S]*(?=\-A\d+)/'; if(preg_match($pattern, $tshirt, $matches)){ echo '<pre>'.print_r($matches, true).'</pre>'; } // match string which contains at least 9 digits in the end $tshirt = "Test, ... Testzaz ^5 &%, New-Code-Name-of-athe-tshirt-A123456889 Test after"; $pattern = '/[A-Za-z-\S]+(?=\-A\d{9})/'; if(preg_match($pattern, $tshirt, $matches)){ echo '<pre>'.print_r($matches, true).'</pre>'; } Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/#findComment-1363903 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 $str = 'Name-of-the-tshirt-A123456789'; preg_match('/-A([0-9]+)$/', $str, $match); echo $match[1]; // match[1] will now hold your ID, in this case 123456789 Link to comment https://forums.phpfreaks.com/topic/266144-help-with-explode/#findComment-1363939 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.