Jump to content

How do I extract partial string from a full string?


Go to solution Solved by NotionCommotion,

Recommended Posts

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
  • Solution

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);

 

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? 

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.

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.

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.

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.

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.