Hello:

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

$from

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.

Mossa

Recommended Answers

All 3 Replies

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.

<?php
    $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)

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

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

$new_from = substr($from, 8);

this worked as specified.

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.