List Info

Thread: note 72611 added to function.preg-replace




note 72611 added to function.preg-replace
user name
2007-01-26 00:32:00
I recently needed the ability to grab only a selected portion of a huge urchin url. While the url is ever changing it's near impossible to guess what exactly to remove, however, the urchin tags stay the same (no guessing). With that, I was happy to see many helpful hints here that lead me to this solution (perhaps helpful to others). This code could probably be written shorter, or cleaner, but I'm still learning all the fun stuff. is travelEverywhere+banner+travelSpecial //for tracking, record keeping and it's easier on the eyes // after calling that url with: $_SERVER['HTTP_REFERER'] $siterefname = $_SERVER['HTTP_REFERER']; // 1st strip the url up to the start of the urchin code "?" // (you can reference these tags in the url above) and put the // remainder back into var $siterefname $siterefname = preg_replace('/http(.*?)utmcsr%3D/i', '', $siterefname); // 2nd stripthe code after the 2nd urchin tag area (which // happens to be after the value in the url you want to keep) // all the way to the last urchin tag in your url (if this // changes, then I will need to call on help or research further // to figure out how to replace all (thinking (.*) after this 2nd // urchin tag with ' ') $siterefname = preg_replace('/%7Cutmccn(.*?)utmk=/i', '', $siterefname); // lastly remove the left over numbers (from the last urchin // tag removed) at the end of the text area (the text you // wanted to keep.) Since the url numbers are never the // same and cannot really be guessed for a more accurate // removal, I can only figure to remove what I knew stayed // the same (the last urchin tag utmk=) // this step might not be necessary if I can figure out how to // remove all after the 2nd urchin tag $siterefname = preg_replace("/([0-9])/", '',$siterefname); // after all that you should be left with only: // "travelEverywhere+banner+travelSpecial", no quotes of // course, unless you want to add them. ;) ?> ---- Server IP: 216.194.113.175 Probable Submitter: 24.17.235.127 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/72611 Del: integrated -- https://master.php.net/note/delete/72611/integrated Del: useless -- https://master.php.net/note/delete/72611/useless Del: bad code -- https://master.php.net/note/delete/72611/bad+code Del: spam -- https://master.php.net/note/delete/72611/spam Del: non-english -- https://master.php.net/note/delete/72611/non-english Del: in docs -- https://master.php.net/note/delete/72611/in+docs Del: other reasons-- https://master.php.net/note/delete/72611 Reject -- https://master.php.net/note/reject/72611 Search -- https://master.php.net/manage/user-notes.php -- PHP Notes Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
note 72611 deleted from function.preg-replace by nlopess
user name
2007-01-31 07:26:33
Note Submitter:  

----

I recently needed the ability to grab only a selected
portion
of a huge urchin url.  While the url is ever changing it's
near
impossible to guess what exactly to remove, however, the
urchin tags stay the same (no guessing).  With that, I was
happy to see many helpful hints here that lead me to this
solution (perhaps helpful to others).

This code could probably be written shorter, or cleaner,
but
I'm still learning all the fun stuff.   

<?php

// assume your urchin code looks like this:
// 'http://www.domain
.com/dir/index.php
// __utma=1.1268841507.1169760188.1169760188.116976018
// .1&__utmb=1&__utmc=1&__utmx=
// &__utmz=1.1169760188.1.1.utmcsr
// 3DtravelEverywhere+banner+travelSpecial%7Cutmccn
// 3DTravelSpecial%7Cutmcmd%3DBanner&__utmv=
// &__utmk=62320813';

//YUK!!  this is horrible to look at, and I only want the
utmsrc
//which <above> is
travelEverywhere+banner+travelSpecial
//for tracking, record keeping and it's easier on the eyes

// after calling that url with:  $_SERVER['HTTP_REFERER']

$siterefname = $_SERVER['HTTP_REFERER'];

// 1st strip the url up to the start of the urchin code
"?"
// (you can reference these tags in the url above) and put
the
// remainder back into var $siterefname

$siterefname = preg_replace('/http(.*?)utmcsr%3D/i', '',
$siterefname);

// 2nd stripthe code after the 2nd urchin tag area (which
// happens to be after the value in the url you want to
keep)
// all the way to the last urchin tag in your url  (if this
// changes, then I will need to call on help or research
further
// to figure out how to replace all (thinking (.*) after
this 2nd
// urchin tag with ' ')

$siterefname = preg_replace('/%7Cutmccn(.*?)utmk=/i', '',
$siterefname);

// lastly remove the left over numbers (from the last
urchin
// tag removed) at the end of the text area (the text you
// wanted to keep.)  Since the url numbers are never the
// same and cannot really be guessed for a more accurate
// removal, I can only figure to remove what I knew stayed
// the same (the last urchin tag utmk=)

// this step might not be necessary if I can figure out how
to 
// remove all after the 2nd urchin tag

$siterefname = preg_replace("/([0-9])/",
'',$siterefname);

// after all that you should be left with only:
// "travelEverywhere+banner+travelSpecial", no
quotes of
// course, unless you want to add them.  ;)

?>

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )