Regex check string

How can check in regex if my post tile == “No Items” and tile lenght is higher 5 character

Thank you fit help

Why would you do that using regex?

Here is the code

$xml = simplexml_load_file($feed_url);
foreach($xml->channel->item as $item)
{ 
    $item->title = "No Items For Such Parameters."; 
    if ($item->title == "No Items")
        continue;

    $check_title=get_page_by_title($item->title, 'OBJECT', 'release');
    if (empty($check_title)) {
        $data = array(
            'post_title'    => $item->title,
            'post_date'     => date('Y-m-d H:i:s',strtotime($item->pubDate)),
            'post_content'  => $item->description,
        );
            wp_insert_post($data);
    
    }
}

Okay, that’s the code that isn’t working? Right?

So what you really want is code that checks if the title starts with “No Items”? and its length must be at least 6 characters?

Yes i like to check if the title starts with “No Items” and its length must be at least 6 characters.

Thank u for Help

$xml = simplexml_load_file($feed_url);
foreach($xml->channel->item as $item)
{ 
    $item->title = "No Items For Such Parameters."; 
    if (strlen($item->title) <= 5 && strpos(strtolower($item->title), "no items") === 0)
        continue;

    $check_title=get_page_by_title($item->title, 'OBJECT', 'release');
    if (empty($check_title)) {
        $data = array(
            'post_title'    => $item->title,
            'post_date'     => date('Y-m-d H:i:s',strtotime($item->pubDate)),
            'post_content'  => $item->description,
        );
            wp_insert_post($data);

    }
}

Okay, so here is the line I updated:

if (strlen($item->title) <= 5 && strpos(strtolower($item->title), "no items") === 0)

The strlen is string length, so if the length of the title is less than or equal to 5, continue to the next item. Then the latter part checks if the position of the string “no items” is found at position 0 (the start of the string), continue to the next item.

1 Like

Thank you @cpradio

@cpradio i will check it does not work

here is the clean code please check

$values[] = array('title'=>'Welcome to the machine is my world');
$values[] = array('title'=>'No Items For Such Parameters.');
$values[] = array('title'=>'Helle word it is programming');
$values[] = array('title'=>'PHP is a great computer language ');
foreach($values as $value){
if (strlen($value['title']) <= 5 && strpos(strtolower($value['title']), "No Items") === 0)
	continue;
       	var_dump($value['title']);

}

string(26) “Bla Bla skkjkskkjs sllkls”
string(29) “No Items For Such Parameters.”
string(25) “Bla Bla hlklkklk lkhkkl 2”
string(26) “Bla Bla llkkjkjgjd skkk 3”

You left “No Items” with capitals, my code doesn’t. Put the N and I as lowercase and it should work because I convert your title to lowercase before doing the compare. So only change the IF statement to have a lower case n and i

Sorry I see now! Thank u work good

1 Like
if (strlen($item->title) <= 5 && strpos(strtolower($item->title), "no items") === 0)

Surely that should be using || rather than &&.

With && you’ll only continue when (impossibly) the title is less than or equal to five characters and starts with “no items” (more than five characters).

2 Likes

Yep, you are correct. I think it was when I was doing a different check and I forgot to swamp it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.