Convert number to words - php

How can I convert my amount into words? I tried this code but it gave me this error when I call the function.
Fatal error: Call to undefined function Convert()
Did I do something wrong?
if (isset($_REQUEST["generate"])) {
$amount=$_POST["amount"];
$amountinword=Convert($amount);
}
<?php
Class Convert {
var $words = array();
var $places = array();
var $amount_in_words;
var $decimal;
var $decimal_len;
function Convert($amount, $currency="Pesos") {
$this->assign();
$temp = (string)$amount;
$pos = strpos($temp,".");
if ($pos) {
$temp = substr($temp,0,$pos);
$this->decimal = strstr((string)$amount,".");
$this->decimal_len = strlen($this->decimal) - 2;
$this->decimal = substr($this->decimal,1,$this->decimal_len+1);
}
$len = strlen($temp)-1;
$ctr = 0;
$arr = array();
while ($len >= 0) {
if ($len >= 2) {
$arr[$ctr++] = substr($temp, $len-2, 3);
$len -= 3;
} else {
$arr[$ctr++] = substr($temp,0,$len+1);
$len = -1;
}
}
$str = "";
for ($i=count($arr)-1; $i>=0; $i--) {
$figure = $arr[$i];
$sub = array(); $temp="";
for ($y=0; $y<strlen(trim($figure)); $y++) {
$sub[$y] = substr($figure,$y,1);
}
$len = count($sub);
if ($len==3) {
if ($sub[0]!="0") {
$temp .= ((strlen($str)>0)?" ":"") . trim($this->words[$sub[0]]) . " Hundred";
}
$temp .= $this->processTen($sub[1], $sub[2]);
} elseif ($len==2) {
$temp .= $this->processTen($sub[0], $sub[1]);
} else {
$temp .= $words[$sub[0]];
}
if (strlen($temp)>0) {
$str .= $temp . $this->places[$i];
}
}
$str .= " " . $currency;
if ($this->decimal_len>0) {
$str .= " And " . $this->decimal . "/" . $this->denominator($this->decimal_len+1) . " Cents";
}
$this->amount_in_words = $str;
}
function denominator($x) {
$temp = "1";
for ($i=1; $i<=$x; $i++) {
$temp .= "0";
}
return $temp;
}
function display() {
echo $this->amount_in_words;
}
function processTen($sub1, $sub2) {
if ($sub1=="0") {
if ($sub2=="0") {
return "";
} else {
return $this->words[$sub2];
}
} elseif ($sub1!="1") {
if ($sub2!="0") {
return $this->words[$sub1."0"] . $this->words[$sub2];
} else {
return $this->words[$sub1 . $sub2];
}
} else {
if ($sub2=="0") {
return $this->words["10"];
} else {
return $this->words[$sub1 . $sub2];
}
}
}
function assign() {
$this->words["1"] = " One"; $this->words["2"] = " Two";
$this->words["3"] = " Three"; $this->words["4"] = " Four";
$this->words["5"] = " Five"; $this->words["6"] = " Six";
$this->words["7"] = " Seven"; $this->words["8"] = " Eight";
$this->words["9"] = " Nine";
$this->words["10"] = " Ten"; $this->words["11"] = " Eleven";
$this->words["12"] = " Twelve"; $this->words["13"] = " Thirten";
$this->words["14"] = " Fourten"; $this->words["15"] = " Fiften";
$this->words["16"] = " Sixten"; $this->words["17"] = " Seventen";
$this->words["18"] = " Eighten"; $this->words["19"] = " Nineten";
$this->words["20"] = " Twenty"; $this->words["30"] = " Thirty";
$this->words["40"] = " Forty"; $this->words["50"] = " Fifty";
$this->words["60"] = " Sixty"; $this->words["70"] = " Seventy";
$this->words["80"] = " Eighty"; $this->words["90"] = " Ninety";
$this->places[0] = ""; $this->places[1] = " Thousand";
$this->places[2] = " Million"; $this->places[3] = " Billion";
$this->places[4] = " Thrillion";
}
}
?>

Problem
You aren't creating an instance of Convert so PHP thinks it's a function.
Solution:
$convert = new Convert($amount);
$convert->display();
On a side note, your Convert method is acting as a constructor function. This is a backwards compatibility function from older versions of PHP. You should follow best-practices by renaming it to __construct now. From the manual:
Warning: Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.
Working example.
Also on a side note - you allow for the currency to be provided as an argument to the constructor method, so you should also allow for the decimal point name as well (Cents) - it's currently hard coded into your Convert method.

Related

make first letter caps in php but ucfirst(strtolower('string')) does not work

I've been trying to make the first letter of a string in the capital but I can't get it to work.
I have tried the following code:
<?php
$str = $_POST['Papier'];
$f = highlightKeywords('papierwaren', $str);
$s = strtolower($f);
$r = ucfirst($s);
function highlightKeywords($text, $keyword)
{
$pos = strpos($text, $keyword);
$wordsAry = explode(" ", $keyword);
$wordsCount = count($wordsAry);
for ($i = 0; $i < $wordsCount; $i++) {
if ($pos === false) {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . strtolower($wordsAry[$i]) . "</span>";
} else {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . $wordsAry[$i] . "</span>";
}
$text = str_ireplace($wordsAry[$i], $highlighted_text, $text);
}
return $text;
}
still, I am not getting it to work and I tried if whitespace occurs with the following
$r=ucfirst(trim($s));
still not succeeded. This 'papierwaren' text i'm getting it form db so pls someone help me to resolve this.
As Kaddath said, You are adding HTML to your string (<span ...). When you use ucfirst it changes the first char to uppercase but the first char is now <, the uppercase for < is <.
Try this code:
<?php
$str = 'papier';
$f = highlightKeywords('papierwaren', $str);
echo $f;
function highlightKeywords($text, $keyword)
{
$pos = strpos($text, $keyword);
$wordsAry = explode(" ", $keyword);
$wordsCount = count($wordsAry);
for ($i = 0; $i < $wordsCount; $i++) {
if ($pos === false) {
if ($i === 0) {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . ucfirst(strtolower($wordsAry[$i])) . "</span>";
} else {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . strtolower($wordsAry[$i]) . "</span>";
}
} else {
if ($i === 0) {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . ucfirst($wordsAry[$i]) . "</span>";
} else {
$highlighted_text = "<span style='font-weight:700;color:#151313;'>" . $wordsAry[$i] . "</span>";
}
}
$text = str_ireplace($wordsAry[$i], $highlighted_text, $text);
}
return $text;
}
In laravel this should help
use Illuminate\Support\Str;
$testString = 'this is a sentence.';
$uppercased = Str::ucfirst($testString);
Do not forget to import Illuminate\Support\Str to controller.

Object of class Convert could not be converted to string amount in words

i used this code to call a function that will convert my number into words words but when i added it on database it gave me error like this : Object of class Convert could not be converted to string did i do wrong thanks for help.
class Convert {
var $words = array();
var $places = array();
var $amount_in_words;
var $decimal;
var $decimal_len;
function __construct($amount, $currency = "Pesos") {
$this->assign();
$temp = (string)$amount;
$pos = strpos($temp,".");
if ($pos) {
$temp = substr($temp,0,$pos);
$this->decimal = strstr((string)$amount,".");
$this->decimal_len = strlen($this->decimal) - 2;
$this->decimal = substr($this->decimal,1,$this->decimal_len+1);
}
$len = strlen($temp)-1;
$ctr = 0;
$arr = array();
while ($len >= 0) {
if ($len >= 2) {
$arr[$ctr++] = substr($temp, $len-2, 3);
$len -= 3;
} else {
$arr[$ctr++] = substr($temp,0,$len+1);
$len = -1;
}
}
$str = "";
for ($i=count($arr)-1; $i>=0; $i--) {
$figure = $arr[$i];
$sub = array(); $temp="";
for ($y=0; $y<strlen(trim($figure)); $y++) {
$sub[$y] = substr($figure,$y,1);
}
$len = count($sub);
if ($len==3) {
if ($sub[0]!="0") {
$temp .= ((strlen($str)>0)?" ":"") . trim($this->words[$sub[0]]) . " Hundred";
}
$temp .= $this->processTen($sub[1], $sub[2]);
} elseif ($len==2) {
$temp .= $this->processTen($sub[0], $sub[1]);
} else {
$temp .= $words[$sub[0]];
}
if (strlen($temp)>0) {
$str .= $temp . $this->places[$i];
}
}
$str .= " " . $currency;
if ($this->decimal_len>0) {
$str .= " And " . $this->decimal . " Cents";
}
$this->amount_in_words = $str;
}
function denominator($x) {
$temp = "1";
for ($i=1; $i<=$x; $i++) {
$temp .= "0";
}
return $temp;
}
function display() {
echo $this->amount_in_words;
}
function processTen($sub1, $sub2) {
if ($sub1=="0") {
if ($sub2=="0") {
return "";
} else {
return $this->words[$sub2];
}
} elseif ($sub1!="1") {
if ($sub2!="0") {
return $this->words[$sub1."0"] . $this->words[$sub2];
} else {
return $this->words[$sub1 . $sub2];
}
} else {
if ($sub2=="0") {
return $this->words["10"];
} else {
return $this->words[$sub1 . $sub2];
}
}
}
function assign() {
$this->words["1"] = " One"; $this->words["2"] = " Two";
$this->words["3"] = " Three"; $this->words["4"] = " Four";
$this->words["5"] = " Five"; $this->words["6"] = " Six";
$this->words["7"] = " Seven"; $this->words["8"] = " Eight";
$this->words["9"] = " Nine";
$this->words["10"] = " Ten"; $this->words["11"] = " Eleven";
$this->words["12"] = " Twelve"; $this->words["13"] = " Thirten";
$this->words["14"] = " Fourten"; $this->words["15"] = " Fiften";
$this->words["16"] = " Sixten"; $this->words["17"] = " Seventen";
$this->words["18"] = " Eighten"; $this->words["19"] = " Nineten";
$this->words["20"] = " Twenty"; $this->words["30"] = " Thirty";
$this->words["40"] = " Forty"; $this->words["50"] = " Fifty";
$this->words["60"] = " Sixty"; $this->words["70"] = " Seventy";
$this->words["80"] = " Eighty"; $this->words["90"] = " Ninety";
$this->places[0] = ""; $this->places[1] = " Thousand";
$this->places[2] = " Million"; $this->places[3] = " Billion";
$this->places[4] = " Thrillion";
}
}
$convert = new Convert($Amount);
$convert->display(); echo " Only";
$sql = "INSERT INTO vouchercheck (Payee,CheckNo,Date1,AmountinWord,Amount) VALUES ('$Payee','$CheckNo','$Date','".$convert."','$Amount')";
We need the code for your Convert class.
With that said, the obvious error being returned is that you are trying to concatenate an "object" (your $convert variable is an object of the Convert class) with a string.
Your Convert class should have a method like "display" that returns the "AmountinWord" string you are expecting.
Display might be that class method, although you should NOT be using "echo" in that method, but rather you should be using return $somevalue;
In that case you would want:
$sql = "INSERT INTO vouchercheck (Payee,CheckNo,Date1,AmountinWord,Amount) VALUES ('$Payee','$CheckNo','$Date','" . $convert->display() . "','$Amount')";
An alternative method to use in your Convert class, is to define the "magic method" __toString. This comes from the Java world, where classes often have a toString method defined that will render a string compatible version of an object. So in your class you would want something like:
public function __toString()
{
return $this->amount_in_words;
}
If you have this method, then you would be able to use the existing code you had:
$sql = "INSERT INTO vouchercheck (Payee,CheckNo,Date1,AmountinWord,Amount) VALUES ('$Payee','$CheckNo','$Date','".$convert."','$Amount')";
Full Class code will now be:
class Convert {
var $words = array();
var $places = array();
var $amount_in_words;
var $decimal;
var $decimal_len;
function __construct($amount, $currency = "Pesos") {
$this->assign();
$temp = (string)$amount;
$pos = strpos($temp,".");
if ($pos) {
$temp = substr($temp,0,$pos);
$this->decimal = strstr((string)$amount,".");
$this->decimal_len = strlen($this->decimal) - 2;
$this->decimal = substr($this->decimal,1,$this->decimal_len+1);
}
$len = strlen($temp)-1;
$ctr = 0;
$arr = array();
while ($len >= 0) {
if ($len >= 2) {
$arr[$ctr++] = substr($temp, $len-2, 3);
$len -= 3;
} else {
$arr[$ctr++] = substr($temp,0,$len+1);
$len = -1;
}
}
$str = "";
for ($i=count($arr)-1; $i>=0; $i--) {
$figure = $arr[$i];
$sub = array(); $temp="";
for ($y=0; $y<strlen(trim($figure)); $y++) {
$sub[$y] = substr($figure,$y,1);
}
$len = count($sub);
if ($len==3) {
if ($sub[0]!="0") {
$temp .= ((strlen($str)>0)?" ":"") . trim($this->words[$sub[0]]) . " Hundred";
}
$temp .= $this->processTen($sub[1], $sub[2]);
} elseif ($len==2) {
$temp .= $this->processTen($sub[0], $sub[1]);
} else {
$temp .= $words[$sub[0]];
}
if (strlen($temp)>0) {
$str .= $temp . $this->places[$i];
}
}
$str .= " " . $currency;
if ($this->decimal_len>0) {
$str .= " And " . $this->decimal . " Cents";
}
$this->amount_in_words = $str;
}
function __toString() {
return $this->amount_in_words;
}
function denominator($x) {
$temp = "1";
for ($i=1; $i<=$x; $i++) {
$temp .= "0";
}
return $temp;
}
function display() {
echo $this->amount_in_words;
}
function processTen($sub1, $sub2) {
if ($sub1=="0") {
if ($sub2=="0") {
return "";
} else {
return $this->words[$sub2];
}
} elseif ($sub1!="1") {
if ($sub2!="0") {
return $this->words[$sub1."0"] . $this->words[$sub2];
} else {
return $this->words[$sub1 . $sub2];
}
} else {
if ($sub2=="0") {
return $this->words["10"];
} else {
return $this->words[$sub1 . $sub2];
}
}
}
function assign() {
$this->words["1"] = " One"; $this->words["2"] = " Two";
$this->words["3"] = " Three"; $this->words["4"] = " Four";
$this->words["5"] = " Five"; $this->words["6"] = " Six";
$this->words["7"] = " Seven"; $this->words["8"] = " Eight";
$this->words["9"] = " Nine";
$this->words["10"] = " Ten"; $this->words["11"] = " Eleven";
$this->words["12"] = " Twelve"; $this->words["13"] = " Thirten";
$this->words["14"] = " Fourten"; $this->words["15"] = " Fiften";
$this->words["16"] = " Sixten"; $this->words["17"] = " Seventen";
$this->words["18"] = " Eighten"; $this->words["19"] = " Nineten";
$this->words["20"] = " Twenty"; $this->words["30"] = " Thirty";
$this->words["40"] = " Forty"; $this->words["50"] = " Fifty";
$this->words["60"] = " Sixty"; $this->words["70"] = " Seventy";
$this->words["80"] = " Eighty"; $this->words["90"] = " Ninety";
$this->places[0] = ""; $this->places[1] = " Thousand";
$this->places[2] = " Million"; $this->places[3] = " Billion";
$this->places[4] = " Thrillion";
}
}

php array values not being pushed onto associative array

I wrote a javascript function which takes in a string and parses it as an associative array.
function getValues(string){
var array_values = new Array();
var pairs_array = string.split('\n');
if(pairs_array[0] == 'SUCCESS'){
window.success = true;
}
for(x=1; x< pairs_array.length; x++){
var parsedValue = '';
//console.log(pairs_array[x] + "<br>");
var pair = pairs_array[x].split('=');
//console.log(pair[1]);
var variable = pair[0];
if(pair[1]){
var value = pair[1];
for(i=0; i< value.length; i++){
var character = value.charAt(i);
if(character == '+'){
parsedValue = parsedValue + character.replace('+', ' ');
}else{
parsedValue = parsedValue + value.charAt(i);
}
}
array_values[variable] = decodeURIComponent(parsedValue);
}else{
array_values[variable] = '';
}
}
return array_values;
}
Then the function is called on the string window.name_value_pairs as follows
var array_callback = getValues(window.name_value_pairs);
for(x in array_callback){
console.log("call" + x + " " + array_callback[x]);
}
Works fine. Now i have been trying to write the function in php because i would prefer it on the server side but it is not working out. I'm not sure if the array values ar getting pushed onto the array because nothing gets returned. heres the php code i have tried:
Note: $results_values is a string
$result_values = $_REQUEST['result_values'];
echo "php array " . getValuesPhp($result_values);
function getValuesPhp($string){
$array_values = array();
$pairs_array = explode("\n",$string);
if($pairs_array[0] == 'SUCCESS'){
$success = true;
echo "TRUE";
}
for($x=1; $x< count($pairs_array); $x++){
$parsedValue = '';
$pair = explode("=",$pairs_array[$x]);
$variable = $pair[0];
if(isset($pair[1])){
$value = $pair[1];
for($i=0; $i< strlen($value); $i++){
$character = $value[$i];
//echo "char \n" . $character;
if(strpos($character, '+') !== false){
//echo "plus";
$parsedValue .= str_replace('+', ' ', $character);
}else{
//echo "hi2";
$parsedValue .= $value[$i];
}
}
echo "\n var " . $variable;
echo "\n parsed " . $parsedValue;
$array_values['" . $variable . "'] = $parsedValue;
//echo "arrayValues " . $array_values['" . $variable . "'];
//array_push($GLOBALS[$array_values]['" . $variable . "'], $parsedValue);
}else{
$array_values['" . $variable . "'] = '';
//array_push($GLOBALS[$array_values]['" . $variable . "'], '');
}
}
//echo "array payment stat" . $array_values['payment_status'];
return $array_values;
}
note: where it says $array_values['" . $variable . "'] this does print out the write result as it goes through the loop however it seems like the array elements are not being added to the array as nothing is returned at the end.
Thanks for any help
Sarah
Update:
#ChrisWillard I would like to return an associative array from the string. the string is in the format where each line is in the form key=value .. it is actually the string which comes back from a paypal pdt response. For example:
SUCCESS
mc_gross=3.00
protection_eligibility=Eligible
address_status=confirmed
item_number1=3
tax=0.00
item_number2=2
payer_id=VWCYB9FFJ
address_street=1+Main+Terrace
payment_date=14%3A26%3A14+May+22%2C+2014+PDT
payment_status=Completed
charset=windows-1252
address_zip=W12+4LQ
mc_shipping=0.00
mc_handling=0.00
first_name=Sam
address_country_code=GB
address_name=Sam+Monks
custom=
payer_status=verified
business=mon%40gmail.com
address_country=United+Kingdom
num_cart_items=2
mc_handling1=0.00
mc_handling2=0.00
address_city=Wolverhampton
payer_email=monks%40gmail.com
mc_shipping1=0.00
mc_shipping2=0.00
tax1=0.00
tax2=0.00
txn_id=3PX5572092U
payment_type=instant
last_name=Monks
address_state=West+Midlands
item_name1=Electro
receiver_email=mon%40gmail.com
item_name2=Dub
quantity1=1
quantity2=1
receiver_id=WHRPZLLP6
pending_reason=multi_currency
txn_type=cart
mc_gross_1=1.00
mc_currency=USD
mc_gross_2=2.00
residence_country=GB
transaction_subject=
payment_gross=3.00
thanks for all your answers and help. it was a combination of two things that caused it to not print.. firstly my silly syntax error (being just new at programming haha I wont go into the logic i had behind this but it did make sense to me at the time haha) $array_values['" . $variable . "'] = $parsedValue; changed to this:
$array_values[$variable] = $parsedValue;
it was also the line
echo "php array" . getValuesPhp($result_values); that caused it not to print.
when i changed this to
print_r(getValuesPhp($result_values)); it printed perfect thanks to #ChrisWillard for this. So here is my final code. A combination of #ChrisWillard answer and #Mark B and #Jdo answers. I also wanted to check first if pair[1] existed and go through each character of pair[1] changing any '+' to a space ' ' if it existed so that it could be read by the user. Now i have found the function to do this for me haha. I'm sure it is not new information for a lot of you but for anyone who doesn't know it is urldecode so you can see below ive commented out the loop that i did not need (going through the characters of the string changing the plus value) and instead ive written: $finished_array[$key] = urldecode($value); thanks for all your help.
$result_values = $_REQUEST['result_values'];
print_r(getValuesPhp($result_values));
function getValuesPhp($string){
$finished_array = array();
$pairs_array = explode("\n",$string);
if($pairs_array[0] == 'SUCCESS'){
$success = true;
//echo "TRUE";
}
for($x=1; $x< count($pairs_array); $x++){
$parsedValue = '';
$pair = explode("=",$pairs_array[$x]);
$key = $pair[0];
if(isset($pair[1])){
$value = $pair[1];
//for($i=0; $i< strlen($value); $i++){
//$character = $value[$i];
//if(strpos($character, '+') !== false){
//$parsedValue .= str_replace('+', ' ', $character);
//}else{
//$parsedValue .= $value[$i];
//}
//}
$finished_array[$key] = urldecode($value);
}else{
$finished_array[$key] = '';
}
}
return $finished_array;
}
This is totally non-sensical:
$array_values['" . $variable . "'] = $parsedValue;
You're literally using " . $variable . " as your array key - remember that '-quoted strings do NOT expand variables.
Why not just
$array_values[$variable] = $parsedValue
From what I can gather, this should get you what you need:
$result_values = $_REQUEST['result_values'];
print_r(getValuesPhp($result_values));
function getValuesPhp($string){
$finished_array = array();
$pairs_array = explode("\n",$string);
if($pairs_array[0] == 'SUCCESS'){
$success = true; ////Not sure what you're trying to do here
}
for($x=1; $x< count($pairs_array); $x++) {
$pair = explode("=",$pairs_array[$x]);
$key = $pair[0];
$value = $pair[1];
$finished_array[$key] = $value;
}
return $finished_array;
}

How To Change Numbers Based On Results

I have a follow up question on something I got help with here the other day (No Table Three Column Category Layout).
The script is as follows:
$res = mysql_query($query);
$system->check_mysql($res, $query, __LINE__, __FILE__);
$parent_node = mysql_fetch_assoc($res);
$id = (isset($parent_node['cat_id'])) ? $parent_node['cat_id'] : $id;
$catalist = '';
if ($parent_node['left_id'] != 1)
{
$children = $catscontrol->get_children_list($parent_node['left_id'], $parent_node['right_id']);
$childarray = array($id);
foreach ($children as $k => $v)
{
$childarray[] = $v['cat_id'];
}
$catalist = '(';
$catalist .= implode(',', $childarray);
$catalist .= ')';
$all_items = false;
}
$NOW = time();
/*
specified category number
look into table - and if we don't have such category - redirect to full list
*/
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE cat_id = " . $id;
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$category = mysql_fetch_assoc($result);
if (mysql_num_rows($result) == 0)
{
// redirect to global categories list
header ('location: browse.php?id=0');
exit;
}
else
{
// Retrieve the translated category name
$par_id = $category['parent_id'];
$TPL_categories_string = '';
$crumbs = $catscontrol->get_bread_crumbs($category['left_id'], $category['right_id']);
for ($i = 0; $i < count($crumbs); $i++)
{
if ($crumbs[$i]['cat_id'] > 0)
{
if ($i > 0)
{
$TPL_categories_string .= ' > ';
}
$TPL_categories_string .= '' . $category_names[$crumbs[$i]['cat_id']] . '';
}
}
// get list of subcategories of this category
$subcat_count = 0;
$query = "SELECT * FROM " . $DBPrefix . "categories WHERE parent_id = " . $id . " ORDER BY cat_name";
$result = mysql_query($query);
$system->check_mysql($result, $query, __LINE__, __FILE__);
$need_to_continue = 1;
$cycle = 1;
$column = 1;
$TPL_main_value = '';
while ($row = mysql_fetch_array($result))
{
++$subcat_count;
if ($cycle == 1)
{
$TPL_main_value .= '<div class="col'.$column.'"><ul>' . "\n";
}
$sub_counter = $row['sub_counter'];
$cat_counter = $row['counter'];
if ($sub_counter != 0)
{
$count_string = ' (' . $sub_counter . ')';
}
else
{
if ($cat_counter != 0)
{
$count_string = ' (' . $cat_counter . ')';
}
else
{
$count_string = '';
}
}
if ($row['cat_colour'] != '')
{
$BG = 'bgcolor=' . $row['cat_colour'];
}
else
{
$BG = '';
}
// Retrieve the translated category name
$row['cat_name'] = $category_names[$row['cat_id']];
$catimage = (!empty($row['cat_image'])) ? '<img src="' . $row['cat_image'] . '" border=0>' : '';
$TPL_main_value .= "\t" . '<li>' . $catimage . '' . $row['cat_name'] . $count_string . '</li>' . "\n";
++$cycle;
if ($cycle == 7) // <---- here
{
$cycle = 1;
$TPL_main_value .= '</ul></div>' . "\n";
++$column;
}
}
if ($cycle >= 2 && $cycle <= 6) // <---- here minus 1
{
while ($cycle < 7) // <---- and here
{
$TPL_main_value .= ' <p> </p>' . "\n";
++$cycle;
}
$TPL_main_value .= '</ul></div>'.$number.'
' . "\n";
}
I was needing to divide the resulting links into three columns to fit my html layout.
We accomplished this by changing the numbers in the code marked with "// <---- here".
Because the amount of links returned could be different each time, I am trying to figure out how to change those numbers on the fly. I tried using
$number_a = mysql_num_rows($result);
$number_b = $number_a / 3;
$number_b = ceil($number_b);
$number_c = $number_b - 1;
and then replacing the numbers with $number_b or $number_c but that doesn't work. Any ideas?
As mentioned before, you can use the mod (%) function to do that.
Basically what it does is to get the remainder after division. So, if you say 11 % 3, you will get 2 since that is the remainder after division. You can then make use of this to check when a number is divisible by 3 (the remainder will be zero), and insert an end </div> in your code.
Here is a simplified example on how to use it to insert a newline after every 3 columns:
$cycle = 1;
$arr = range (1, 20);
$len = sizeof ($arr);
for ( ; $cycle <= $len; $cycle++)
{
echo "{$arr[$cycle - 1]} ";
if ($cycle % 3 == 0)
{
echo "\n";
}
}
echo "\n\n";

extract part of a string before and after a word

i need to extract and show some words before and after a query word, something like google search results, for example:
$str = "hi user! welcome to new php open source world, we are trying to learn you something!";
$query = "new php";
$result = "... welcome to new php open source ...";
i searched google an SO but didn't find a clear answer or maybe my php knowledge was not enough!
is there a workable and easy-to-use function to do this job?
function yourFuncName($str, $query, $numOfWordToAdd) {
list($before, $after) = explode($query, $str);
$before = rtrim($before);
$after = ltrim($after);
$beforeArray = array_reverse(explode(" ", $before));
$afterArray = explode(" ", $after);
$countBeforeArray = count($beforeArray);
$countAfterArray = count($afterArray);
$beforeString = "";
if($countBeforeArray < $numOfWordToAdd) {
$beforeString = implode(' ', $beforeArray);
}
else {
for($i = 0; $i < $numOfWordToAdd; $i++) {
$beforeString = $beforeArray[$i] . ' ' . $beforeString;
}
}
$afterString = "";
if($countAfterArray < $numOfWordToAdd) {
$afterString = implode(' ', $afterArray);
}
else {
for($i = 0; $i < $numOfWordToAdd; $i++) {
$afterString = $afterString . $afterArray[$i] . ' ';
}
}
$string = $beforeString . $query . ' ' . $afterString;
return $string;
}
Output is: user! welcome to new php open source world, ($numOfWordToAdd = 3)
Here is an working example I thing that it is clear what I did and how:
<?php
$str = "hi user! welcome to new php open source world, we are trying to learn you something!";
$query = "new php";
$expl = explode($query, $str);
// items on the left side of middle string
$expl_left = explode(" ", $expl[0]);
$left_cnt = count($expl_left);
$new_left = $expl_left[$left_cnt-3] . " " . $expl_left[$left_cnt-2];
// items on the right side of middle string
$expl_right = explode(" ", $expl[1]);
$new_right = $expl_right[1] . " " . $expl_right[2];
// new string formated
$new = "... " . $new_left . " " . $query . " " . $new_right . " ...";
print $new;
?>
If you have some questions feel free to ask...
$result = preg_replace('/(.+)?([^\s]+.{10}'.$query.'.{10}[^\s]+)(.+)?/', '... $2 ...', $str);
This will return the same result from the same string and query you gave. If the before or after length starts or ends (respectively) in the middle of a word, it will continue until it completes the word before it stops.
Assuming a "word" is any series of non-whitespace characters, the following will extract 3 words on either side of new php out of the string $subject, but accept less if necessary:
if (preg_match('/(?:\S+\s+){1,3}new php(?:\s+\S+){1,3}/', $subject, $regs)) {
$result = $regs[0];
}
Change the 3s to any number you like.
I used the following function with explode:
public static function returnSearch($query, $str, $wordcount) {
$explode = explode($query, $str);
$result = null;
//if explode count is one the query was not found
if (count($explode) == 1) {
$result = implode(' ', array_slice(str_word_count($explode[0], 2), -$wordcount, $wordcount)) . " ";
}
//if explode count is more than one the query was found at least one time
if (count($explode) > 1) {
//check for if the string begins with the query
if (!empty($explode[0])) {
$result = "..." . implode(' ', array_slice(str_word_count($explode[0], 2), -$wordcount, $wordcount)) . " ";
}
$result = $result . $query;
if (!empty($explode[1])) {
$result = $result . " " . implode(' ', array_slice(str_word_count($explode[1], 2), 0, $wordcount)) . "...";
}
}
//return result
return $result;
}
Corrected function from #Can Vural, it wont mess the phrase of the before match and its case insensitive, very usefull to dispaly in php search results:
function render_search_words($str, $query, $numOfWordToAdd) {
list($before, $after) = preg_split("/$query/i", $str);
$before = rtrim($before);
$after = ltrim($after);
$beforeArray = explode(" ", $before);
$afterArray = explode(" ", $after);
$countBeforeArray = count($beforeArray);
$countAfterArray = count($afterArray);
$beforeString = "";
if($countBeforeArray < $numOfWordToAdd) {
$beforeString = implode(' ', $beforeArray);
}
else {
for($i = 0; $i < $numOfWordToAdd; $i++) {
$beforeString = $beforeArray[$i] . ' ' . $beforeString;
}
}
$afterString = "";
if($countAfterArray < $numOfWordToAdd) {
$afterString = implode(' ', $afterArray);
}
else {
for($i = 0; $i < $numOfWordToAdd; $i++) {
$afterString = $afterString . $afterArray[$i] . ' ';
}
}
$string = '...'.$beforeString . ' <span>' . $query . '</span> ' . $afterString.'...';
return $string;
}

Categories