filter specific string in php
$var="UseCountry=1 UseCountryDefault=1 UseState=1 UseStateDefault=1 UseLocality=1 UseLocalityDefault=1 cantidad_productos=5 expireDays=5 apikey=ABQIAAAAFHktBEXrHnX108wOdzd3aBTupK1kJuoJNBHuh0laPBvYXhjzZxR0qkeXcGC_0Dxf4UMhkR7ZNb04dQ distancia=15 AutoCoord=1 user_add_locality=0 SaveContactForm=0 ShowVoteRating=0 Listlayout=0 WidthThumbs=100 HeightThumbs=75 WidthImage=640 HeightImage=480 ShowImagesSystem=1 ShowOrderBy=0 ShowOrderByDefault=0 ShowOrderDefault=DESC SimbolPrice=$ PositionPrice=0 FormatPrice=0 ShowLogoAgent=1 ShowReferenceInList=1 ShowCategoryInList=1 ShowTypeInList=1 ShowAddressInList=1 ShowContactLink=1 ShowMapLink=1 ShowAddShortListLink=1 ShowViewPropertiesAgentLink=1 ThumbsInAccordion=5 WidthThumbsAccordion=100 HeightThumbsAccordion=75 ShowFeaturesInList=1 ShowAllParentCategory=0 AmountPanel= AmountForRegistered=5 RegisteredAutoPublish=1 AmountForAuthor=5 AmountForEditor=5 AmountForPublisher=5 AmountForManager=5 AmountForAdministrator=5 AutoPublish=1 MailAdminPublish=1 DetailLayout=0 ActivarTabs=0 ActivarDescripcion=1 ActivarDetails=1 ActivarVideo=1 ActivarPanoramica=1 ActivarContactar=1 ContactMailFormat=1 ActivarReservas=1 ActivarMapa=1 ShowImagesSystemDetail=1 WidthThumbsDetail=120 HeightThumbsDetail=90 idCountryDefault=1 idStateDefault=1 ms_country=1 ms_state=1 ms_locality=1 ms_category=1 ms_Subcategory=1 ms_type=1 ms_price=1 ms_bedrooms=1 ms_bathrooms=1 ms_parking=1 ShowTextSearch=1 minprice= maxprice= ms_catradius=1 idcatradius1= idcatradius2= ShowTotalResult=1 md_country=1 md_state=1 md_locality=1 md_category=1 md_type=1 showComments=0 useComment2=0 useComment3=0 useComment4=0 useComment5=0 AmountMonthsCalendar=3 StartYearCalendar=2009 StartMonthCalendar=1 PeriodOnlyWeeks=0 PeriodAmount=3 PeriodStartDay=1 apikey=ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2Y开发者_如何学PythoniIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ ";
in that string only i want "api==ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ";
plz guide me correctly;
EDIT
As shamittomar pointed out, the parse_str will not work for this situation, posted the proper regex below.
Given this seems to be a QUERY STRING
, use the parse_str() function PHP provides.
UPDATE
If you want to do it with regex using preg_match() as powertieke pointed out:
preg_match('/apikey=(.*)/', $var, $matches);
echo $matches[1];
Should do the trick.
preg_match(); should be right up your alley
people are so fast to jump to preg match when this can be done with regular string functions thats faster.
$string = '
expireDays=5
apikey=ABQIAAAAFHktBEXrHnX108wOdzd3aBTupK1kJuoJNBHuh0laPBvYXhjzZxR0qkeXcGC_0Dxf4UMhkR7ZNb04dQ
distancia=15
AutoCoord=1';
//test to see what type of line break it is and explode by that.
$parts = (strstr($string,"\r\n") ? explode("\r\n",$string) : explode("\n",$string));
$data = array();
foreach($parts as $part)
{
$sub = explode("=",trim($part));
if(!empty($sub[0]) || !empty($sub[1]))
{
$data[$sub[0]] = $sub[1];
}
}
and use $data['apikey']
for your api key, i would also advise you to wrpa in function.
I can bet this is a better way to parse the string and much faster.
function ParsemyString($string)
{
$parts = (strstr($string,"\r\n") ? explode("\r\n",$string) : explode("\n",$string));
$data = array();
foreach($parts as $part)
{
$sub = explode("=",trim($part));
if(!empty($sub[0]) || !empty($sub[1]))
{
$data[$sub[0]] = $sub[1];
}
}
return $data;
}
$data = ParsemyString($string);
First of all, you are not looking for
api==ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ
but you are looking for
apikey=ABQIAAAAJ879Hg7OSEKVrRKc2YHjixSmyv5A3ewe40XW2YiIN-ybtu7KLRQiVUIEW3WsL8vOtIeTFIVUXDOAcQ
It is important to know if the api-key property always occurs at the end and if the length of the api-key value is always the same. I this is the case you could use the PHP substr()
function which would be easiest.
If not you would most probably need a regular expression which you can feed to PHPs preg_match()
function. Something along the lines of apikey==[a-zA-Z0-9\-]
Which matches an api-key containing a-z in both lowercase and uppercase and also allows for dashes in the key. If you are using the preg_match()
function you can retrieve the matches (and thus your api-key value).
精彩评论