Jump to content

Recommended Posts

Hi i'm trying to edit a WP Custom function but i'm stumped.

 

I'm using the following function currently to remove everything in a string up to and including ","

 

Is it possible to edit it to remove everything AFTER and including ","

 

Or is there a better alternative ?

function checkstr($x){
    if(strpos($x,',')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x, strpos($x,',')),',');
    }
}
Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/
Share on other sites

Thanks for your reply, i haven't been able to test it because i am already using the other function so i get an error

Fatal error: Cannot redeclare checkstr() (previously declared in

 what would be the best way to achieve what i need .. i actually need to be able to call the 2 functions seperately .. here is what i have at the moment, which obviously causes the error.

/**
 * Get Color From Title
 */
function checkstr($x){
    if(strpos($x,',')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x, strpos($x,',')),',');
    }
}

/**
 * Remove Color From Title
 */
function checkstr($x){
    if(strpos($x,',')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x, 0, strpos($x,',')),',');
    }
}

Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469163
Share on other sites

I should probably provide more information, i am using WP All Import to import a product csv.

 

The Product titles appear like so : Crossover Crop Top In Burgundy, Burgundy

 

So my first function allows me to extract "Burgundy" from the end to use as Category/Color

 

My second function should strip ",Burgundy" from the end to leave me Crossover Crop Top In Burgundy to use as Product Title.

 

As these are pulled into different fields i can use the same column name with multiple functions, unfortunately it would appear that i can't declare the same function twice in function.php.

Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469166
Share on other sites

Well i can do that and that seems like the perfect option but is it as simple as renaming the function like so ?

function this_is_my_other_checkstr($x){
    if(strpos($x,',')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x, 0, strpos($x,',')),',');
    }
}

Thanks for your input !

Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469172
Share on other sites

 

 

i am using WP All Import to import a product csv.

Why not use str_getcsv?

$string = 'Crossover Crop Top In Burgundy, Burgundy';
list($title, $other) = str_getcsv($string);

echo 'Text before comma: ' . $title. '<br />';
echo 'Text after comma: '. $other;
Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469175
Share on other sites

Thanks guys, this is now working for me, but i need to add to it do also remove the word "Large" from the start of the string, it is consistantly the first word in the string and it is the same word always.

 

What would be the best way to add it to this function to do both.

function other_checkstr($x){
    if(strpos($x,'-')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x,0, strpos($x,'-')),'-');
    }
}
Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469412
Share on other sites

So i somehow need to combine this 

function other_checkstr($x){
    if(strpos($x,'-')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x,0, strpos($x,'-')),'-');
    }
}

and this

str_replace("Large","","$x");
   

Ive tried multiple combinations, but i can't seem to get it to work. Can anybody help me please ?

 

Thank you !

Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469721
Share on other sites

Yeh i have been reading the manual, which is how i got this far. Ive also been looking at other functions to try and understand how they can be combined. I tried this

function other_checkstr($x){
    str_replace("Large","",$x);
    if(strpos($x,'-')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x,0, strpos($x,'-')),'-');
    }
}

The function runs the second part successfully as required, but the str_replace has no effect.

Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469742
Share on other sites

Then you haven't read the manual properly. Str_replace returns a string with values replaced. You are not picking up the returned value.

function other_checkstr($x){
    $x = str_replace("Large","",$x);
    if(strpos($x,'-')==FALSE){
        return trim(substr(strrchr($x, ' '), 1 ));  
    }else{
        return trim(substr($x,0, strpos($x,'-')),'-');
    }
}
Link to comment
https://forums.phpfreaks.com/topic/286237-custom-function/#findComment-1469748
Share on other sites

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.