Forum Moderators: coopster

Message Too Old, No Replies

simple str replace question

         

auxiv

2:06 pm on Jan 23, 2010 (gmt 0)

10+ Year Member



if im using:


$b = "topic-".<$number-variable>."html";
$a = str_replace($b, "topic_".<$number-variable>."php", $string);
echo $a;

I would like to know what the "<$number-variable>" <---- would be?

so if/when $string contains in this case: topic-23423.html (23423 is hypothetical) it will convert it over to: topic_23423.php. however the number string will change periodically, and I need php to expect a number range? whats the php code for <$number-variable> in this case?

auxiv

2:49 pm on Jan 23, 2010 (gmt 0)

10+ Year Member



This would be a wildcard regular expression

auxiv

3:14 pm on Jan 23, 2010 (gmt 0)

10+ Year Member



cant use regular expressions with str_replace

rocknbil

8:28 pm on Jan 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I'm not sure what you're asking. :-)
I need php to expect a number range? whats the php code for <$number-variable> in this case?

Like this?


if ($number_variable > 0) {
$b = "topic-".$number_variable."html";
$a = str_replace($b, "topic_".$number_variable."php", $string);
}

Unless you expect to have a file topic_0.html, use > 0 rather than is_numeric().
If you're doing what I **think** you're doing, Consider,


$request = 'example.com/products/12345'; // Etc., the page request
$full_page = '/server/path/to/directory';
$dir = '/products'; // Or use explode below to parse
$vars = explode('/',$request);
$number_variable = array_pop($vars); // Last item in array
if ($number_variable > 0) {
$page = 'topic_' . $number_variable . '.php';
if (is_file("$full_path/$page")) {
header("Location:$dir/$page");
}
else { "echo invalid file request"; exit; }
}
else { echo "Invalid page request"; }

auxiv

10:47 pm on Jan 23, 2010 (gmt 0)

10+ Year Member



Thanks for your response, and time you took to help rocknbil. Will try.