Re: preview string with strlen PHP (help)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dwayne Heronimo wrote:
> Dear All,
> 
> I am very new to programming. I want to make a preview text that would 
> display only a part of the text that is a text field in a database.
> 
> //Begin Make preview string
> 
>  $minitxt = $row_show_cat['text'];
>  $len = strlen($minitxt);
> 
>  if ($len > 235)
>  {
>   $len = 235;
>  }
>  else
>  {
>   $len = $len;
>  }
> 
>  $newstring = substr($minitxt,0,$len);
> 
>  $previewtext = $newstring;
> 
> //End Make preview string
> 
> But if I want to display this somewhere in the page it will only display the 
> first record of at every text column row <?php echo $previewstext ?>
> 
> Althought I am very new to php and programming. I thought maybe I should 
> rewrite it into a function. But my function won't work.
> 
> 
> function previewString($showcatvar) {
> 
>  $minitxt = $showcatvar;
>  $len = strlen($minitxt);
> 
>  if ($len > 235)
>  {
>   $len = 235;
>  }
>  else
>  {
>   $len = $len;
>  }
> 
>  $newstring = substr($minitxt,0,$len);
> 
>  $previewtext = $newstring;

the above variable assignment is superfluous.

> 
>  $previewtext = $whowcatvar;

the above variable assignment is very wrong
(unless you intend to set $previewtext to NULL right after
you have just done the 'hard' work of creaating it).

> 

you have no return statement in your function so it will never give you
a value - some other languages implicitly return the value of the last expression
in a function (e.g. Ruby IIRC) but php requires that you explicitly
use 'return' to pass a variable back to the code that called the function.

note also that the variable named inside the function have nothing to do with
variables named outside the function - this is called variable scope ...
you can reference a variable in the global scope (i.e. one defined in
the outer most level of code by using the 'global' keyword), an example

<?php

function myFunc() {
	global $foo;

	$bar = "boohoo :-(";

	echo " ...INSIDE: ", $foo, " ", $bar;
}

$foo = "tada!";
$bar = "haha!";

myFunc();
echo " ...OUTSIDE: ", $foo, " ", $bar;

?>
> }

have a look at this:

function previewString($showcatvar)
{
	// sanity check on the input
	if (empty($showcatvar) || !is_string($showcatvar))
		return "";

	// get the string size
 	$len = strlen($showcatvar);

	// long strings will be truncated to 235 chars
	// BTW - this will really bite you in the ass if
	// the string contains HTML (think about unclosed tags!)
 	if ($len > 235)
		return substr($showcatvar, 0, 235);

	// strings less than or equal to 235 in length
	// are shown as is
 	return $showcatvar;
}

> 
> 
> and to display it in the page:
> 
> <?php echo  previewString($row_show_cat['text']) ?>
> 
> IT displays notthing. But I think I am doing somthing wrong. I am assigning 
> the wrong variables to the $row_show_cat['text']) ???
> 
> Please let me know 
> 

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux