I'm trying to strip a word out of a variable and echo it out. here is what I mean:


the value of which is INTERVAL 7 DAY

I have this line that includes the variable:

echo"The total number of transaction(s) found within the last $from: "; echo $row["numrecords"]; echo"<br/>";

Currently this line would echo as:

"The total number of transaction(s) found within the last INTERVAL 7 DAY: 25"

What I really want to happen is:

"The total number of transaction(s) found within the last 7 DAY: 25"

Essentially I want to strip INTERVAL out at the echo line. Is this doable? If so any guidance is most appreciative.


Edited by mbarandao: n/a

6 Years
Discussion Span
Last Post by mbarandao

Yes this is possible. But the solution will depend on whether the $from variable always starts with INTERVAL, or whether it changes...

If it is always going to start with INTERVAL, then you could use a simple substr() function to cut off the first word. This simply says to print everything after the 8th character, counting I as 0, N as 1, T as 2 etc.

    $new_from = substr($from, 8);
    echo $new_from;

The syntax for substr is

string substr ( string $string , int $start [, int $length ] )

Alternatively, if the first word changes you could look into using explode() (using space as the delimiter)

// Example
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2 

Edited by nonshatter: n/a


Thanks for the reply. Your response is quite detailed, I appreciate this. I'll attempt the solution and advise of the outcome. Again thanks!

$new_from = substr($from, 8);

this worked as specified.

Thanks again!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.