The issue: I need to check a string for key phrases and if it contains those phrases, assign to a certain tag name.
I'm working in WordPress with PHP.
The code that I've created so far is:
/* WOMENS SPORTSWEAR TAGS */
function map_womens_sportswear_tags( $tag_name ) {
$tags = array(
'Womens Tracksuit Pants' => array(
'Tracksuit Pants',
'Sweatpants',
'Shell Pants',
'Jogger Pants',
'Jogging Bottoms',
),
'Womens Tracksuit Jackets' => array(
'Tracksuit Jacket',
),
'Womens Fleece Jacket' => array(
'Fleece',
),
);
foreach( $tags as $tag => $values ) {
$current_tag = $tag; // Current tag is the name of the tag, like Womens Sports Shoe or Womens Tracksuit Pants
foreach( $values as $value ) {
if ( strtolower( $value ) == strtolower( $tag_name ) || stripos( $tag_name, $current_tag ) ) {
return $current_tag; // Returns tag name, i.e. 'Womens Sports Shoe' or 'Womens Tracksuit Pants'
break;
}
}
}
return $tag_name;
}
Therefore, if a string contains the words Jogging Bottoms, assign to Womens Tracksuit Pants. I've used stripos for this purpose, however I can't get the code to work.
Please could someone point me in the right direction as I'm new to coding. Many thanks in advance.
It seems you just mixed up your variabled in the if statement:
if ( strtolower( $value ) == strtolower( $tag_name ) || stripos( $tag_name, $current_tag ) ) {
should be
if ( strtolower( $value ) == strtolower( $tag_name ) || stripos( $tag_name, $value ) ) {