In buddypress plugin subject is printed using this code
<?php bp_the_thread_subject(); ?>
at my end this function/method is printing result like Hello world: 1236 . I want to take only integer value from this. But when i am applying anything to this function/method, it is getting zero result. How to do that ?
You can use the filter_var function to extract the number
<?php
function bp_the_thread_subject(){
//All your logic to return the string....
return 'Hello world: 1236';
}
$value = filter_var(bp_the_thread_subject(), FILTER_SANITIZE_NUMBER_INT);
echo $value // 1236;
?>
Assuming that bp_the_thread_subject()
returns Hello world: 1236
, when can use filter_var
with FILTER_SANITIZE_NUMBER_INT
to remove everything that's not an int
.
$identity = filter_var( bp_the_thread_subject(), FILTER_SANITIZE_NUMBER_INT );
$myInt = (int)$identity;
var_dump($myInt);
//int(1236)
You can use preg_match
to return only the number:
<?php preg_match('/\d+/', bp_the_thread_subject(), $matches)[1] ?>