imgrooot Posted August 21, 2016 Share Posted August 21, 2016 Below I have "Input" where it shows the original string. Basically I want to remove everything before " - ". I would like to know how can I get the "Output" results using PHP? Input $variable_1 = Lulu-free - Swimsuit swimwear red $variable_2 = Pap & Jones - Bard Mini Dress Output $variable_1 = Swimsuit swimwear red $Variable_2 = Bard Mini Dress Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/ Share on other sites More sharing options...
NotionCommotion Posted August 21, 2016 Share Posted August 21, 2016 Is it fair to say that " - " will always separate the two strings? Will a hyphen ever be in the second string part? Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536406 Share on other sites More sharing options...
Solution NotionCommotion Posted August 21, 2016 Solution Share Posted August 21, 2016 Maybe? <?php function getIt($var) { $pos = strpos($var, ' - '); echo(substr($var, $pos+3)."\n"); } $variable_1 = 'Lulu-free - Swimsuit swimwear red'; $variable_2 = 'Pap & Jones - Bard Mini Dress'; getIt($variable_1); getIt($variable_2); Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536407 Share on other sites More sharing options...
imgrooot Posted August 21, 2016 Author Share Posted August 21, 2016 Maybe? <?php function getIt($var) { $pos = strpos($var, ' - '); echo(substr($var, $pos+3)."\n"); } $variable_1 = 'Lulu-free - Swimsuit swimwear red'; $variable_2 = 'Pap & Jones - Bard Mini Dress'; getIt($variable_1); getIt($variable_2); That works perfectly. One more thing. Basically I have a long list of items in MySQL database with the title in the format I specified above. Basically what I am trying to do is do 2 things. 1. Remove everything before the " - " to create a new title. This works thanks to you. 2. The string that is removed before the " - ", I want to add it to a new table column. Now my next question is, how do I get these strings(Lulu-free, Pap & Jones) as a variable? Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536408 Share on other sites More sharing options...
NotionCommotion Posted August 21, 2016 Share Posted August 21, 2016 Are you doing this just once to fix a badly designed table? If not, you shouldn't be doing this. To do the next step, just do it the same way. Figure out where your deliminator position is using strpos(), and use substr() to return the correct string. Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536409 Share on other sites More sharing options...
imgrooot Posted August 21, 2016 Author Share Posted August 21, 2016 Are you doing this just once to fix a badly designed table? If not, you shouldn't be doing this. To do the next step, just do it the same way. Figure out where your deliminator position is using strpos(), and use substr() to return the correct string. I think I have it working for the 2nd step. $variable_1 = 'Lulu-free - Swimsuit swimwear red'; $variable_2 = 'Pap & Jones - Bard Mini Dress'; $str1 = substr($variable_1, 0, strpos($variable_1, ' - ')); // results: Lulu-free $str2 = substr($variable_2, 0, strpos($variable_2, ' - ')); // results: Pap & Jones No unfortunately it is not once but several hundred rows. Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536414 Share on other sites More sharing options...
NotionCommotion Posted August 21, 2016 Share Posted August 21, 2016 Good job getting the second step working. Understand that it will be several hundred rows, but are you doing so to fix the table? Your database should have a table for the company and a table for the product, and the application should create the combined string. Your database should not directly be saving the combined string. Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536415 Share on other sites More sharing options...
imgrooot Posted August 21, 2016 Author Share Posted August 21, 2016 Good job getting the second step working. Understand that it will be several hundred rows, but are you doing so to fix the table? Your database should have a table for the company and a table for the product, and the application should create the combined string. Your database should not directly be saving the combined string. Yes I am doing it so that in the future I can I filter out products by "Brand" name. Currently the brand name is directly in the title. I can still have it in the title when I output the variables but in the database, it would be better to have it in an individual column. Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536417 Share on other sites More sharing options...
NotionCommotion Posted August 21, 2016 Share Posted August 21, 2016 Yes I am doing it so that in the future I can I filter out products by "Brand" name. Currently the brand name is directly in the title. I can still have it in the title when I output the variables but in the database, it would be better to have it in an individual column. Good Quote Link to comment https://forums.phpfreaks.com/topic/301957-how-do-i-extract-partial-string-from-a-full-string/#findComment-1536419 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.