Forum Moderators: coopster

Message Too Old, No Replies

Concatenate a Variable?

         

brotherhood of LAN

5:40 pm on Sep 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Say I wanted to use a URI as a variable.... say /111.htm

Can I concatenate that to 111 on its own?

The variable can then be used in a mysql query to match a primary key...

jatar_k

5:58 pm on Sep 26, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do you mean cut it?

I don't know if I understand what you are asking BOL.

url like /111.htm read the num out of it "111" and then use that num to make a DB call?

If so, yes, no problem.

brotherhood of LAN

6:02 pm on Sep 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yeah, the idea being just to request the URL and concatenate it to match the primary key...its for that directory.

Any chance you can squeeze in the code? :)

jatar_k

6:39 pm on Sep 26, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe something like this?

<?
$string = "http://www.something.com/dir1/111.htm";
$pattern = '^.*/([^\./]*)\..{3,4}$';
eregi($pattern, $string, $vars);
echo $vars[1];
?>

brotherhood of LAN

6:50 pm on Sep 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



heh, bang on as usual....thanks chief.

With this sorted I can do that word id thing, hopefully post the results when i finish :)

andreasfriedrich

6:50 pm on Sep 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since Perl is a lot nicer than PHP we need to use at least Perl-Compatible regular expressions in PHP ;)

preg_match("'/(\d+)\.([a-z]{3,4})$'i", $url, $matches); 
$number = $matches[0];

Andreas

brotherhood of LAN

10:04 pm on Sep 30, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




$string = "$REQUEST_URI";
$pattern = '^.*/([^\./]*)\..{3,4}$';
eregi($pattern, $string, $vars);
echo $vars[1];

Ok, just to go a step further on this, I'm using apache and have heard the wonders of mod_rewrite, and I wonder if it can be used here for some neato reason ;)

The URL used above will be a plain old number, no query string...just grabbing the number of the file. This number will match a word in a db.....is there a surefire way of converting the number into the word related to it in the db? I think mod_rewrite is the answer here, I'm new to apache and wouldnt know where to start apart from inklings from old threads.

ergophobe

10:54 pm on Sep 30, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




need to use at least Perl-Compatible regular expressions in PHP

More importantly, the PCRE are actually faster than the POSIX-based regex in PHP, even though it seems counter-intuitive.

The speed gain is not huge, but it's a simple 1-for-1 change that *does* give you an efficiency gain.

Tom

brotherhood of LAN

11:16 pm on Sep 30, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When trying what andreas suggests I get back the same value as I would using $REQUEST_URI, ie with / and .htm included.

This leaves me a bit confused, if Andreas' method is more efficient then how do I get it to do what jatars code done for me first time off ....if anyone knows, I'd be thankful for covering up my ignorance of PHP :)

/added that was with replacing $url, with $request_uri.......seems efficiency is the least of my probs before i get to understand the code better

andreasfriedrich

11:31 pm on Sep 30, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, the number is contained in $matches[1].

$matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

preg_match [php.net]

$url = "http://www.something.com/dir1/111.htm";  
preg_match("'/(\d+)\.[a-z]{3,4}$'i", $url, $matches);
$number = $matches[1];

Andreas

brotherhood of LAN

11:52 pm on Sep 30, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks :)

Birdman

1:57 pm on Nov 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello WW,

I too need the same sort of effect. I'm using an include for my navigation with a call to a db so I can change my menu site wite at once(I love this php/mysql stuff).

Now I'd like to go a step further, and run an if/else on the results so I can match the current page and deactivate the link to the current page.

I'm using this to get the url:

$filelocation=$HTTP_HOST.$PHP_SELF;
echo($filelocation);

What I get from that is:

www.my_site.com/the_page I'm_on.html

I need to cut the actual filename off of the url or is there a way to call for the filename itself?

Thanks

brotherhood of LAN

2:09 pm on Nov 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If your looking for ALL of the path after the domain, you can use $REQUEST_URI

I know there is one for the file on its own, just cant find it...no doubt someone else will post it.

You might also want to use parse_url, which breaks up a URL into fragments. You could use all the fragments of the URL after the host.

Birdman

3:45 pm on Nov 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks BoL! I'll try that and search for 'requesting the filename' command
:)

jatar_k

5:46 pm on Nov 20, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$filename = current(explode('.', basename($SCRIPT_NAME)));

I found this on this page
[php.net...]

andreasfriedrich

6:14 pm on Nov 20, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adamīs code will give you the_page_I'm_on.

If you donīt want the extension stripped off use:

substr($_SERVER[REQUEST_URI], strrpos($_SERVER[REQUEST_URI], '/')+1);

This will give you the_page_I'm_on.html.

Andreas