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
Share on other sites

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 );

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.