• Resolved faeronsayn

    (@faeronsayn)


    I just need some php code that would be able to filter out the numbers from the post title in a loop

    For example this loop here

    <?php foreach($posts as $post)
    { … } ?>

    My post titles look like the following

    blah blah blah blah blah blah 24
    blah blah blah blah 123-124
    blah blah blah blah blah 23

    These numbers will always appear at the end of the post title.

    So basically if there is a function that will look for the first number it finds in the post title, and then print everything after that would definitely work for my scenario.

    I tried using the substr function, but that won’t work when you have different amounts of digits to print.

    There’s always the preg_match, but it’s not showing me the results I want when I use a code like the following

    <?php $posttitle = the_title(”,”,false);
    $pattern = ‘/[0-9]/’;
    preg_match($pattern, $posttitle, $matches);
    echo $matches[0];
    ?>

Viewing 1 replies (of 1 total)
  • Thread Starter faeronsayn

    (@faeronsayn)

    Found the answer.. just a little modification to the above code.

    Instead of using the_title() use the_title_attribute()

    so

    <?php $posttitle = the_title_attribute(‘echo=0’);
    $pattern = ‘/[0-9]/’;
    preg_match($pattern, $posttitle, $matches);
    echo $matches[0];
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘How to filter out the numbers from a post title’ is closed to new replies.