Jump to content

[SOLVED] Getting Certain Part of String


cheechm

Recommended Posts

Hi,

I have this string:

/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839

(and many more like it). I was wondering how I could get "Lighting". I specifically want it so that it looks for "Lighting" after the = sign after "category". Ie, like $_GET['category'], just I can't do that.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/118350-solved-getting-certain-part-of-string/
Share on other sites

http://us.php.net/manual/en/function.preg-match.php

 

preg_match("/category=(.*)\b/", $stringVar, $matches);
$matches[1];

 

$matches[1] will be "Lighting"

 

This is off the top of my head (not sure if I used word boundry right), but there is the man page link also.

It's at least a start.

You can use strstr to isolate just the query as well

 

<?php

# Untested

$str = '/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839';

$query = substr( strstr($str, '?'), 1 );

parse_str( $query, $query_array );

print_r( $query_array );

?>

here's my take:

<?php
$url = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839";
$url_query = parse_url($url);
parse_str($url_query['query']);
echo $category;
?>

 

This way will create an un-needed array, and generally take more processing power than basic string comparisons... but yes, it will work :D

here's my take:

<?php
$url = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839";
$url_query = parse_url($url);
parse_str($url_query['query']);
echo $category;
?>

 

This way will create an un-needed array, and generally take more processing power than basic string comparisons... but yes, it will work :D

 

You know, according to the manual, parse_url is supposed to accept a second argument to only return the part you want (as a string), but when I tried to add PHP_URL_QUERY as the 2nd argument, php kept screaming at me about how parse_url only accepts one argument...so...am I just not understanding the manual right, or is the manual wrong?

 

edit:

 

Oh wait, I just read further down: the component argument is a feature of php5 I was testing it on a php4 server. whoops  :o

I am using this one:

 

<?php
$str = "/server/lhs/admin/pages/4.php?act=1&step=2&category=Lighting&1217980250839";
$str = substr($str, strpos($str, '?') + 1);
parse_str($str, $output);
echo $output['category'];
?>

 

However, the plus sign doesn't seem to work. If the string contains a plus sign, for instance "Lighting+", then the plus doesn't show. Please help!

 

 

Thanks

Plus signs are converted to ' ' by parse_str.

 

urlencode will conver it to it's hex equivalent (%2B)...

 

In order to get the exact string-to-string conversion, you'll have to urlencode the string before parse_str, and urldecode after...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.