• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

php commands for isolating parts of a string

fuzzybabybunny

Moderator<br>Digital & Video Cameras
Moderator
I'm looking for a way to do this in php:

1. Read the file name of the php page it's on. Ex. publish_3.php

2. Store the filename as a variable. Ex. $page_id = "publish_3.php"

3. $page_id will then be used to set styles for that page ie. navigation hover-over tick marks

4. Find the number in the $page_id variable and assign THAT to another variable. Ex. "publish_3.php" has a number of 3. $page_num = "3".

5. $page_num will then be used to set styles for that page ie. navigation hover-over tick marks
 
I did this:

<?php
$page_id = basename ($_SERVER["PHP_SELF"]);
$page_cat = strstr($page_id, '_', true);
print $page_id;
print $page_cat;
?>

But now I need to isolate parts of the filename: I want to break down something like "publish_3.php" into a page_category of "publish" and a page_number of "3."

I've tried strstr but it's buggy. The above code should have $page_cat as "publish" but instead I get a "Wrong parameter count for strstr()" error. Ppeople say the reason is because strstr should have only two parameters while mine has three, but my example is straight from php.net...

http://us.php.net/strstr

EDIT: looks like the three parameter version is only for PHP v5.3, but the highest version of PHP available is v.5.29. So basically they're publishing specs for a version that doesn't exist yet...
 
Originally posted by: TekniDude
you need php 5.3 or greater for that to work http://us2.php.net/manual/en/function.strstr.php

try this

$page_id = basename($_SERVER["PHP_SELF"]);
list($page_cat,$page_num) = explode("_",substr($page_id,0,strpos($page_id,'.')));

Oooooo.... clever! Thanks so much. This is the kind of thing that's going to get me in the future - learning individual php functions isn't too bad, but learning enough and having the creativity and logic to string them together into one big function is going to be tough 🙁

Lemme decompose this:

ex. $page_id = "images_4.php"

1. strpos gets the position of the "." sign, which is 8.
2. substr($page_id,0,8) returns the portion of the $page_id string between and including 0 and 8, which is "images_4"
3. explode("_" , "images_4") returns "images" and "4" -> does this put them in an array for the list function to use?
4. list($page_cat,$page_num) = "images" "4" is basically $page_cat = "images" and $page_num = "4"
 
Originally posted by: fuzzybabybunny
I'm looking for a way to do this in php:

1. Read the file name of the php page it's on. Ex. publish_3.php

2. Store the filename as a variable. Ex. $page_id = "publish_3.php"

3. $page_id will then be used to set styles for that page ie. navigation hover-over tick marks

4. Find the number in the $page_id variable and assign THAT to another variable. Ex. "publish_3.php" has a number of 3. $page_num = "3".

5. $page_num will then be used to set styles for that page ie. navigation hover-over tick marks

You can use explode() or preg_match(). I prefer preg_match over explode() as it gives finer control over a string.
 
Originally posted by: blahblah99

You can use explode() or preg_match(). I prefer preg_match over explode() as it gives finer control over a string.

preg_match() is much more powerful, but potentially much more complicated. In this specific instance, either method will work fine provided that it can be guaranteed that only one '_' will be in the string. In the long term, it is very valuable to learn how to use preg_match(), even just the basics.
 
regular expressions are great, but still slower than basic string functions. if you can use a str function, do so
 
can also do:
$fname = basename($_SERVER['PHP_SELF'], ".php");
$parts = split("_", $fname)
$page_cat = $parts[0];
$page_id = $parts[1];
 
Back
Top