preview string with strlen PHP (help)

This is a discussion on preview string with strlen PHP (help) within the PHP General forums, part of the PHP Programming Forums category; Dear All, I am very new to programming. I want to make a preview text that would display only a ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-23-2007
Dwayne Heronimo
 
Posts: n/a
Default preview string with strlen PHP (help)

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;

$previewtext = $whowcatvar;

}


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
Reply With Quote
  #2 (permalink)  
Old 03-23-2007
Tijnema !
 
Posts: n/a
Default Re: [PHP] preview string with strlen PHP (help)

On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
>
> $previewtext = $whowcatvar;
>
> }
>
>
> 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


I'm currently not sure why your first piece of code isn't working, but
the second is quite simple. You aren't returning any variable in your
function, and so it won't output anything. Add the following:
return $previewtext;
Just before the }

Then it will return something, but i think not what you wanted to :(

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

Reply With Quote
  #3 (permalink)  
Old 03-23-2007
Flavien CHANTELOT
 
Posts: n/a
Default RE: [PHP] preview string with strlen PHP (help)



-----Message d'origine-----
De*: Tijnema ! [mailto:tijnema@gmail.com]
Envoyé*: vendredi 23 mars 2007 16:26
À*: Dwayne Heronimo
Cc*: php-general@lists.php.net
Objet*: Re: [php] preview string with strlen PHP (help)

On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
>
> $previewtext = $whowcatvar;
>
> }
>
>
> 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


I'm currently not sure why your first piece of code isn't working, but
the second is quite simple. You aren't returning any variable in your
function, and so it won't output anything. Add the following:
return $previewtext;
Just before the }

Then it will return something, but i think not what you wanted to :(

The first piece of code isn't working cause : $previewtext != $previewstext
Tijnema
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Reply With Quote
  #4 (permalink)  
Old 03-23-2007
Edward Kay
 
Posts: n/a
Default RE: [PHP] preview string with strlen PHP (help)


> -----Original Message-----
> From: Dwayne Heronimo [mailto:dwayne@orbitalnets.com]


> 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;
>
> $previewtext = $whowcatvar;
>
> }
>
>
> 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


Hi Dwayne,

Your function isn't working as you expect because you are not returning any
value. I would rewrite it as:

function previewString($strInput) {
// Max length allowed
// You could also store this in a config file or pass in as
// a second, possibly optional argument.
$intMaxLen = 235;

if (strlen($strInput) > $intMaxLen) {
// Too long so trucate it (and add ... to show
// it is truncated. I subtract 3 to allow for ...)
return substr($strInput, 0, ($intMaxLen-3)).'...'
}

// If we get here it is less than or equal to the allowed max so just
return it
return $strInput;
}

You would then call the function when you needed the truncated string, e.g.
<?php echo previewString($previewstext); ?>

If you are outputting this to a web page, you'll probably also want to pass
it through the htmlentities function: <?php echo
htmlentities(previewString($previewstext)); ?>

Good luck,
Edward
Reply With Quote
  #5 (permalink)  
Old 03-23-2007
Jochem Maas
 
Posts: n/a
Default Re: [PHP] preview string with strlen PHP (help)

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
>

Reply With Quote
  #6 (permalink)  
Old 03-23-2007
Dwayne Heronimo
 
Posts: n/a
Default Re: [PHP] preview string with strlen PHP (help)

Dear all,

hmm.. sorry the $previewstext thing was a typo in my mail.

But yes it is working but it will only display the first record of the
recordset. I have like a list for items with short text in a page and of
course made a query to make the database variables available. Which where
the variable $row_show_cat['text']; comes from.

This is why I thought that i may have to rewrite it into a function to
display the $row_show_cat['text']; in a repeated reagion.


I have rewritten my function:

<?php
function previewString($showcatvar) {

$minitxt = $showcatvar;
$len = strlen($minitxt);

if ($len > 235)
{
$len = 235;
}
else
{
$len = $len;
}

$newstring = substr($minitxt,0,$len);

$previewtext = $newstring;

$previewtext = $whowcatvar;

return $previewtext;
}
?>

And to to show it later in the page:
<?php echo previewString($row_show_cat['text']); ?>
but this still won't anywould display anything. :S

Please let me know.

""Tijnema !"" <tijnema@gmail.com> wrote in message
news:d8269d910703230826o39de7e53we1f3ac6267a2f0a4@ mail.gmail.com...
> On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
>>
>> $previewtext = $whowcatvar;
>>
>> }
>>
>>
>> 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

>
> I'm currently not sure why your first piece of code isn't working, but
> the second is quite simple. You aren't returning any variable in your
> function, and so it won't output anything. Add the following:
> return $previewtext;
> Just before the }
>
> Then it will return something, but i think not what you wanted to :(
>
> Tijnema
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>

Reply With Quote
  #7 (permalink)  
Old 03-23-2007
Németh Zoltán
 
Posts: n/a
Default Re: [PHP] preview string with strlen PHP (help)

2007. 03. 23, péntek keltezéssel 16.55-kor Dwayne Heronimo ezt Ã*rta:
> Dear all,
>
> hmm.. sorry the $previewstext thing was a typo in my mail.
>
> But yes it is working but it will only display the first record of the
> recordset. I have like a list for items with short text in a page and of
> course made a query to make the database variables available. Which where
> the variable $row_show_cat['text']; comes from.
>
> This is why I thought that i may have to rewrite it into a function to
> display the $row_show_cat['text']; in a repeated reagion.
>
>
> I have rewritten my function:
>
> <?php
> function previewString($showcatvar) {
>
> $minitxt = $showcatvar;
> $len = strlen($minitxt);
>
> if ($len > 235)
> {
> $len = 235;
> }
> else
> {
> $len = $len;
> }
>
> $newstring = substr($minitxt,0,$len);
>
> $previewtext = $newstring;
>
> $previewtext = $whowcatvar;


what is the meaning of the above line???
you are giving an undefined value (default NULL) to $previewtext, just
after you gave it the right value
delete that line and you might be well

greets
Zoltán Németh

>
> return $previewtext;
> }
> ?>
>
> And to to show it later in the page:
> <?php echo previewString($row_show_cat['text']); ?>
> but this still won't anywould display anything. :S
>
> Please let me know.
>
> ""Tijnema !"" <tijnema@gmail.com> wrote in message
> news:d8269d910703230826o39de7e53we1f3ac6267a2f0a4@ mail.gmail.com...
> > On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
> >>
> >> $previewtext = $whowcatvar;
> >>
> >> }
> >>
> >>
> >> 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

> >
> > I'm currently not sure why your first piece of code isn't working, but
> > the second is quite simple. You aren't returning any variable in your
> > function, and so it won't output anything. Add the following:
> > return $previewtext;
> > Just before the }
> >
> > Then it will return something, but i think not what you wanted to :(
> >
> > Tijnema
> >>
> >> --
> >> PHP General Mailing List (http://www.php.net/)
> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >>
> >>

>

Reply With Quote
  #8 (permalink)  
Old 03-23-2007
Flavien CHANTELOT
 
Posts: n/a
Default RE: [PHP] preview string with strlen PHP (help)

In your function there is at the end before the return statement:
$previewtext = $whowcatvar;
First $whowcatvar isn't defined. So it could be an answer.
And I think u need to delete this line.

-----Message d'origine-----
De*: Dwayne Heronimo [mailto:dwayne@orbitalnets.com]
Envoyé*: vendredi 23 mars 2007 16:55
À*: php-general@lists.php.net
Objet*: Re: [php] preview string with strlen PHP (help)

Dear all,

hmm.. sorry the $previewstext thing was a typo in my mail.

But yes it is working but it will only display the first record of the
recordset. I have like a list for items with short text in a page and of
course made a query to make the database variables available. Which where
the variable $row_show_cat['text']; comes from.

This is why I thought that i may have to rewrite it into a function to
display the $row_show_cat['text']; in a repeated reagion.


I have rewritten my function:

<?php
function previewString($showcatvar) {

$minitxt = $showcatvar;
$len = strlen($minitxt);

if ($len > 235)
{
$len = 235;
}
else
{
$len = $len;
}

$newstring = substr($minitxt,0,$len);

$previewtext = $newstring;

$previewtext = $whowcatvar;

return $previewtext;
}
?>

And to to show it later in the page:
<?php echo previewString($row_show_cat['text']); ?>
but this still won't anywould display anything. :S

Please let me know.

""Tijnema !"" <tijnema@gmail.com> wrote in message
news:d8269d910703230826o39de7e53we1f3ac6267a2f0a4@ mail.gmail.com...
> On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
>>
>> $previewtext = $whowcatvar;
>>
>> }
>>
>>
>> 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

>
> I'm currently not sure why your first piece of code isn't working, but
> the second is quite simple. You aren't returning any variable in your
> function, and so it won't output anything. Add the following:
> return $previewtext;
> Just before the }
>
> Then it will return something, but i think not what you wanted to :(
>
> Tijnema
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Reply With Quote
  #9 (permalink)  
Old 03-23-2007
Dwayne Heronimo
 
Posts: n/a
Default Re: [PHP] preview string with strlen PHP (help)

YES this works thank nemeth:

<?php
function previewString($showcatvar) {

$minitxt = $showcatvar;
$len = strlen($minitxt);

if ($len > 235)
{
$len = 235;
}
else
{
$len = $len;
}

$newstring = substr($minitxt,0,$len);

$previewtext = $newstring;

return $previewtext;
}
?>

and to display it:

<?php echo previewString($row_show_cat['text']); ?>

cheers

Dwayne




""Németh Zoltán"" <znemeth@alterationx.hu> wrote in message
news:1174667018.5030.24.camel@zoli-desktop...
> 2007. 03. 23, péntek keltezéssel 16.55-kor Dwayne Heronimo ezt írta:
>> Dear all,
>>
>> hmm.. sorry the $previewstext thing was a typo in my mail.
>>
>> But yes it is working but it will only display the first record of the
>> recordset. I have like a list for items with short text in a page and of
>> course made a query to make the database variables available. Which where
>> the variable $row_show_cat['text']; comes from.
>>
>> This is why I thought that i may have to rewrite it into a function to
>> display the $row_show_cat['text']; in a repeated reagion.
>>
>>
>> I have rewritten my function:
>>
>> <?php
>> function previewString($showcatvar) {
>>
>> $minitxt = $showcatvar;
>> $len = strlen($minitxt);
>>
>> if ($len > 235)
>> {
>> $len = 235;
>> }
>> else
>> {
>> $len = $len;
>> }
>>
>> $newstring = substr($minitxt,0,$len);
>>
>> $previewtext = $newstring;
>>
>> $previewtext = $whowcatvar;

>
> what is the meaning of the above line???
> you are giving an undefined value (default NULL) to $previewtext, just
> after you gave it the right value
> delete that line and you might be well
>
> greets
> Zoltán Németh
>
>>
>> return $previewtext;
>> }
>> ?>
>>
>> And to to show it later in the page:
>> <?php echo previewString($row_show_cat['text']); ?>
>> but this still won't anywould display anything. :S
>>
>> Please let me know.
>>
>> ""Tijnema !"" <tijnema@gmail.com> wrote in message
>> news:d8269d910703230826o39de7e53we1f3ac6267a2f0a4@ mail.gmail.com...
>> > On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
>> >>
>> >> $previewtext = $whowcatvar;
>> >>
>> >> }
>> >>
>> >>
>> >> 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
>> >
>> > I'm currently not sure why your first piece of code isn't working, but
>> > the second is quite simple. You aren't returning any variable in your
>> > function, and so it won't output anything. Add the following:
>> > return $previewtext;
>> > Just before the }
>> >
>> > Then it will return something, but i think not what you wanted to :(
>> >
>> > Tijnema
>> >>
>> >> --
>> >> PHP General Mailing List (http://www.php.net/)
>> >> To unsubscribe, visit: http://www.php.net/unsub.php
>> >>
>> >>

>>

Reply With Quote
  #10 (permalink)  
Old 03-23-2007
Németh Zoltán
 
Posts: n/a
Default Re: [PHP] preview string with strlen PHP (help)

2007. 03. 23, péntek keltezéssel 17.30-kor Dwayne Heronimo ezt Ã*rta:
> YES this works thank nemeth:


your welcome but please call me Zoltán ;)
(my first name is Zoltán. in Hungary we write names the opposite order
than anywhere else ;) so that's why my mailbox is set to display 'Németh
Zoltán' but I sign my mails as 'Zoltán Németh' showing that my first
name is Zoltán and last name is Németh :) )

greets
Zoltán Németh

>
> <?php
> function previewString($showcatvar) {
>
> $minitxt = $showcatvar;
> $len = strlen($minitxt);
>
> if ($len > 235)
> {
> $len = 235;
> }
> else
> {
> $len = $len;
> }
>
> $newstring = substr($minitxt,0,$len);
>
> $previewtext = $newstring;
>
> return $previewtext;
> }
> ?>
>
> and to display it:
>
> <?php echo previewString($row_show_cat['text']); ?>
>
> cheers
>
> Dwayne
>
>
>
>
> ""Nmeth Zoltn"" <znemeth@alterationx.hu> wrote in message
> news:1174667018.5030.24.camel@zoli-desktop...
> > 2007. 03. 23, pntek keltezssel 16.55-kor Dwayne Heronimo ezt rta:
> >> Dear all,
> >>
> >> hmm.. sorry the $previewstext thing was a typo in my mail.
> >>
> >> But yes it is working but it will only display the first record of the
> >> recordset. I have like a list for items with short text in a page and of
> >> course made a query to make the database variables available. Which where
> >> the variable $row_show_cat['text']; comes from.
> >>
> >> This is why I thought that i may have to rewrite it into a function to
> >> display the $row_show_cat['text']; in a repeated reagion.
> >>
> >>
> >> I have rewritten my function:
> >>
> >> <?php
> >> function previewString($showcatvar) {
> >>
> >> $minitxt = $showcatvar;
> >> $len = strlen($minitxt);
> >>
> >> if ($len > 235)
> >> {
> >> $len = 235;
> >> }
> >> else
> >> {
> >> $len = $len;
> >> }
> >>
> >> $newstring = substr($minitxt,0,$len);
> >>
> >> $previewtext = $newstring;
> >>
> >> $previewtext = $whowcatvar;

> >
> > what is the meaning of the above line???
> > you are giving an undefined value (default NULL) to $previewtext, just
> > after you gave it the right value
> > delete that line and you might be well
> >
> > greets
> > Zoltn Nmeth
> >
> >>
> >> return $previewtext;
> >> }
> >> ?>
> >>
> >> And to to show it later in the page:
> >> <?php echo previewString($row_show_cat['text']); ?>
> >> but this still won't anywould display anything. :S
> >>
> >> Please let me know.
> >>
> >> ""Tijnema !"" <tijnema@gmail.com> wrote in message
> >> news:d8269d910703230826o39de7e53we1f3ac6267a2f0a4@ mail.gmail.com...
> >> > On 3/23/07, Dwayne Heronimo <dwayne@orbitalnets.com> 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;
> >> >>
> >> >> $previewtext = $whowcatvar;
> >> >>
> >> >> }
> >> >>
> >> >>
> >> >> 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
> >> >
> >> > I'm currently not sure why your first piece of code isn't working, but
> >> > the second is quite simple. You aren't returning any variable in your
> >> > function, and so it won't output anything. Add the following:
> >> > return $previewtext;
> >> > Just before the }
> >> >
> >> > Then it will return something, but i think not what you wanted to :(
> >> >
> >> > Tijnema
> >> >>
> >> >> --
> >> >> PHP General Mailing List (http://www.php.net/)
> >> >> To unsubscribe, visit: http://www.php.net/unsub.php
> >> >>
> >> >>
> >>

>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:51 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0