Closed Thread Icon

Topic awaiting preservation: striping url to be used as a $variable Pages that link to <a href="https://ozoneasylum.com/backlink?for=25049" title="Pages that link to Topic awaiting preservation: striping url to be used as a $variable" rel="nofollow" >Topic awaiting preservation: striping url to be used as a $variable\

 
Author Thread
rayzun
Obsessive-Compulsive (I) Inmate

From:
Insane since: Feb 2005

posted posted 02-19-2005 00:04

Any ideas cause Im lost on this one:

url format:
domain.com/abc.1352.html

Im striping parts ".html", "abc"
which only leaves number "1352".

The "1352" is my search criteria:

code:
<?php 
$agent = $_SERVER["REQUEST_URI"];
$agent = str_replace (".html", "", $agent);
$agent = str_replace ("abc", "", $agent);

$lines = file('/home/cgi-bin/cs/data/car.data');
foreach ($lines as $line) {
$parts = explode('|', $line);
if ($parts[0] == $_GET[' 1352 ']) {
echo "$parts[50] ";
}}
?>

this was accomplished before by having a url
domain.com/abc.1352.html?&db_id=1352
withthe script as:
if ($parts[0] == $_GET[' db_id ']) {



but I would like to trim it down a notch.

(Edited by bitdamaged on 02-19-2005 00:56)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-19-2005 01:09

You really need to be more specific about what you are trying to do.

First of all you should use the basename of the PHP_SELF Variable to make sure you're not getting extra characters

$agent = basename($_SERVER['PHP_SELF']);

Now you should have agent = "abc.1352.html'

Now you can get the middle the way you did but you need to remember the first period too
$agent = str_replace (".html", "", $agent);

$agent = str_replace ("abc.", "", $agent);

(there's better ways to do this but I'm not going to worry about them now)

But heck I don't know what the $agent is supposed to do. Restate your question please and don't be stingy with the typing.






domain.com/abc.1352.html



.:[ Never resist a perfect moment ]:.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 02-19-2005 01:14

Hello and welcome in the Asylum,

To extract the parameter for the REQUEST_URI, you should use the parse_url( ) and pathinfo( ) functions to pick the basename of the requested page them, remove the useless parts.

... or you could also use a preg_replace( )

Hope that helps. Anyway do not hesitate to use the search feature and the Frequently Asked Questions of the Asylum. That topic have certainly already been covered.

Nathus
Bipolar (III) Inmate

From: Minnesota
Insane since: Aug 2003

posted posted 02-19-2005 15:09

You could also use

code:
$agent = basename($_SERVER['PHP_SELF']);
$parts = explode(".",$agent);
$agent = $parts[1];



Edit Tyberius Prime: Added some code tags. Otherwise, this plays links your [ 1]...

(Edited by Tyberius Prime on 02-19-2005 16:14)

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 02-21-2005 01:09

The above methods are all well and good, but they dont leave much room for "automation". i.e having something flexible that could deal with many situations.

what if you have def.1234.html, or abcde.1234.html

preg_replace, parse_url arent that useful.

I do this alot and find the best way is to make your own function using thigns such as
substr_count()
strpos()
strrpos()
strlen()

and finish of with
substr()

Basically what you do is get the locations of dots or whatever separater you are using, underscore or anything, then use substr to return a string from a starting position, to a certain length.

in your situation you could do something like

code:
//Get the site page
$PHP_Self = $_SERVER['PHP_SELF'];

function PARSE_MYURL($oldString, $separator)
{
$firstPos = 0; $secondPos = 0; $newString = "";
$firstPos = strpos($oldString, $separator);
$secondPos = strpos($oldString, $separator, $firstPos + 1);

$newString = substr($oldString, $firstPos , strlen($oldString) - $secondPos);
return $newString;
}

print PARSE_MYURL($PHP_Self, ".");



anyhow something like that, just customize it to whatever you need, but try and make it as flexible as possible when you do it. You could have another function variable there for example, so you can specify to return the first element "abc" second element "1234" etc etc.

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 02-21-2005 01:27

H][RO: isn't a

code:
echo preg_replace( "/(\d+)\.html/i", "\\1", basename( $_SERVER['REQUEST_URI'] ) );

not flexible enough

You could also do :

code:
function PARSE_MYURL( $oldString, $separator )
{
if( $tmpArray = explode( $separator, $oldString ) )
return $tmpArray[ max(0,count( $tmpArray )-2 ) ];
return "";
}


Notice, the max(0,count( $tmpArray )-2 ) is just here in case there is less than 2 separators in $oldString.



(Edited by poi on 02-21-2005 01:35)

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 02-21-2005 01:39

Yeh there is a number of ways to do it, some more efficient too - depends on the string and what you are trying to do. Using a mix with preg replace is probably best. The exploding array would work well if you wanted to specify which segment to get also.

When i say not flexible enough i mean replacing abc. etc with preg_replace, since it might change. Depends entirely on the situation really.

is preg_replace more efficient than doing str_replace(".html", "", $string);

i dont use preg_replace nearly as much as i should

« BackwardsOnwards »

Show Forum Drop Down Menu