/    Sign up×
Community /Pin to ProfileBookmark

how to split an url in 2 parts?? (i dont need parse_url)

hello
i would like to split:

[CODE][url]www.site.com/page.php?q=SPLITHERE&cat=0&others=1[/url][/CODE]

into

[CODE][url]www.site.com/page.php?q=[/url][/CODE]

and

[CODE]&cat=0&others=1[/CODE]

how can i do that?
i tried with explode, but it didnt work.

please help ?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@andre4s_yFeb 24.2008 — try :
[code=php]
$output = preg_split("/=/",$string, 2);
//or
$output = preg_split("/(=)/",$string, 2,PREG_SPLIT_DELIM_CAPTURE);
//or
preg_match("/(^.+=)(.+$)/U",$string,$output);
[/code]

That first line code split the $string into array of 2, without include the delimiter.

That second line code split the $string into array of 3, with include the demiliter between.

That third line matching the $string first and capture the subpattern.

For example :
[code=php]
<?php
$string = "www.site.com/page.php?q=SPLITHERE&cat=0&others=1";
$output = preg_split("/=/",$string, 2);
print_r($output);
$output = preg_split("/(=)/",$string, 2,PREG_SPLIT_DELIM_CAPTURE);
print_r($output);
preg_match("/(^.+=)(.+$)/U",$string,$output);
print_r($output);
?>
[/code]

It will output :

Array ( [0] => www.site.com/page.php?q [1] => SPLITHERE&cat=0&others=1 ) Array ( [0] => www.site.com/page.php?q [1] => = [2] => SPLITHERE&cat=0&others=1 ) Array ( [0] => www.site.com/page.php?q=SPLITHERE&cat=0&others=1 [1] => www.site.com/page.php?q= [2] => SPLITHERE&cat=0&others=1 )
[/quote]
Copy linkTweet thisAlerts:
@ZiplineFeb 24.2008 — This may not be the fastest way to get to the result you were looking for but I was able to do it successfully using explode just as a learning exercise. I have included a full sample script of how I did it including output.


[code=php]
<?PHP

/*******************************************************************************

SCRIPT OUTPUTS

Section1: www.site.com/page.php?q=
Section2: SPLITHERE&cat=0&others=1
Item in q=: SPLITHERE
Rest of Querystring: &cat=0&others=1

*******************************************************************************/

//URL TO SPLIT
$url = "www.site.com/page.php?q=SPLITHERE&cat=0&others=1";

//SEPERATE BASED ON ?q= JUST q or ? or = MAY APPEAR ELSEWHERE SO USING "?q=" IS LEAST LIKELY TO FIND A DUPLICATE
$pieces = explode("?q=",$url);

//BUILD THE TWO SECTIONS
$section1 = $pieces[0];
$section2 = $pieces[1];

//DISPLAY SAMPLE USING THE DATA
echo("Section1: {$section1}?q=");
echo("<br />");
echo("Section2: {$section2}");

//NOW WE CAN USE THE SAME TECHNIQUE TO GET RID OF "SPLITHERE"
$pieces2 = explode("&",$section2);

//START A COUNT
$n = 1;

//OPEN SECTION 3
$section3 = "";

//LOOP PIECES
if(is_array($pieces2)) {
foreach($pieces2 as $piece) {
if($n == 1) {
$item = $piece;
} else {
$section3 .= "&{$piece}";
}
$n++;
}
}

//DISPLAY THE RESULTS
echo("<br />");
echo("Item in q=: {$item}");
echo("<br />");
echo("Rest of Querystring: {$section3}");

?>
[/code]
Copy linkTweet thisAlerts:
@NogDogFeb 24.2008 — My suggestion from the [url=http://phpbuilder.com/board/showthread.php?t=10351495]same thread at PHPBuilder.com[/url]:
[code=php]if(preg_match('/^([^=]+=)[^&]*(&.*)?/', $text, $matches))
{
echo $matches[1]."<br />n".$matches[2];
}
else
{
user_error("Match failed");
}[/code]
×

Success!

Help @kejo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...