PHP - string manipulation
PHP is a scripting language ideally suited for manipulating strings. That's why it is used for such activities as web scrapes, text manipulation, and text merges.
What’s in a string?
A string is any variable that is a literal value. In PHP, it is usually contained within quote marks. You can think of a string as just a lump of text.
Here’s an example of a string:
$string = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.";
Printing this variable to the screen by writing the line
echo $string; would display:
Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.
Capitalize the first letter of every word in a PHP string
Now for some fun. PHP is feature rich with comands that assist in string manipulation. For example, if you wanted to capitalise every word in the above $string, all you need do is use the ucwords command
echo ucwords($string);
would give you
Let His Holiness Swami Baktanimbin Relieve You Of The Heavy Burden Of Your Wallet. And The Bad Kharma Of Your Credit Card Details
Capitalize every letter in a PHP string
Perhaps you now want to capitalise every single letter. Using our original $string, all we need do is
echo strtoupper($string);
and we would get
LET HIS HOLINESS SWAMI BAKTANIMBIN RELIEVE YOU OF THE HEAVY BURDEN OF YOUR WALLET. AND THE BAD KHARMA OF YOUR CREDIT CARD DETAILS.
Make every letter in a PHP string lowercase
We can also go the other way. Changing every letter to lowwer case is achieved by
echo strtolower($string);
which gives
let his holiness swami baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.
Replace parts of a PHP string
PHP has the ability to do searcgh and replace functions on the fly. For example, say we had a bit of text which needs a particular word replaced by a number of words.
For example, our original $string, you will recall, outputs
Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.
If we write
$newstring = str_replace("Baktanimbin", "Chanjindapokit Baktanimbin", $string );
echo $newstring;
We get
Let his Holiness Swami Chanjindapokit Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit
card details.You'll notice the str_replace() function takes three parameters, the word to be replaced ("Baktanimbin") the word or words to replace it with ("Chanjindapokit Baktanimbin") and the string you want to manipulate - in this case the string called $string.
Return all data after a given character or string
This is a great technique for web scraping. The function used is strstr() which returns the rest of the string after a particular character or group or characaters (sometimes called a substring) is encountered.
For example:
$string = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your
wallet. and the bad kharma of your credit card details.";$newstring =strstr ($string, "and");
echo($newstring);
Will print
and the bad kharma of your
credit card details.This can be very useful for extarcting text from a web site. If you look at Blueswami's Googlefight page, you can see how this is used to extract data from Google. The PHP code for this page is below.
Googlefight PHP example
$SearchForm .= '<form name="SearchForm" action="" method="post">';
$SearchForm .= '<b>Term 1:</b><input type="text" name="term1" class="text" value="'.$searchString1.'"><br><br>';
$SearchForm .= '<b>Term 2:</b><input type="text" name="term2" class="text" value="'.$searchString2.'"><br />';
$SearchForm .= "<br><input type='submit' name='submit' value='Googlefight it!'>";
$SearchForm .= '</form>';
if (!isset($_POST['submit'])) {
} else {
$strquote="'";
$strplus='+';
$strbr='<br>';
$strscored =" scored a total of ";
$strspace=" ";
$str0='http://www.google.com/search?&q=';
$str1=$strquote.$_POST['term1'].$strquote;
$str2=$strquote.$_POST['term2'].$strquote;
$strA4=$str0.$str1;
$strA5=$str0.$str2;
$str4=str_replace($strspace, "+", $strA4);
$str5=str_replace($strspace, "+", $strA5);
function GetPageData($pageurl)
{ return(file_get_contents($pageurl));
}
function GrabPage($pageurl)
{
$pageurl = GetPageData($pageurl);
$pagecontent = $pageurl;
return($pagecontent);
}
$term1 = GrabPage($str4);
$term2 = GrabPage($str5);
Function Trimit($term)
{
$cutterm = strstr($term, "</b> of about <b>");
$endbit = strstr($cutterm, "</b> for <b>");
$finalterm = str_replace($endbit, "", $cutterm);
$deletethis = array(",", "</b> of about <b>" );
$termfinal = str_replace($deletethis, "", $finalterm);
if ($termfinal==""){$termfinal="0";}
return ($termfinal);
}
$finalfirstterm= Trimit($term1);
$finalsecondterm = Trimit($term2);
if ($finalfirstterm > $finalsecondterm) {$decision = $str1; $loser = $str2;
$winnumber=$finalfirstterm; $losenumber=$finalsecondterm; }
if ($finalsecondterm > $finalfirstterm) {$decision = $str2; $loser = $str1;
$winnumber=$finalsecondterm; $losenumber=$finalfirstterm;
}
if ($winnumber==0){$winnumber=1;}
if ($losenumber==0) {$losenumber=1;}
$awidth=($losenumber*(300/$winnumber));
$lwidth=floor($awidth);
$xwidth=($winnumber/$losenumber)*100;
$ywidth= floor($xwidth)-100;
$endwombat=' wins by '.$ywidth.' per cent';
$head ='<h4><b>'.$str1.' versus '.$str2.'</b></h4><br>';
$head .="<div style="width:300px; height:10px; background-color:#FF0000"></div>".$decision."<br><br>";
$head .="<div style="width:".$lwidth."px; height:10px; background-color:#FF0000"></div>".$loser."<br>";
}
$wombat=$SearchForm.$head.$str1.$strscored.$finalfirstterm.$strbr.$str2.$strscored.$finalsecondterm.$strbr.'<b>'.$decision.$endwombat.'</b>';
return ($wombat);

