Forum Moderators: coopster

Message Too Old, No Replies

Snipping text after certain char

How to cut a snippet of text using php

         

wheelie34

1:37 pm on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I bookmarked a previous thread many months back and over the weekend tried it out but cant get it to work! the original thread wont allow any further posts.

Heres what I have, short_desc comes out of a mysql db and all works fine

$desc = stripslashes($newArray['short_desc']);

function snip()
{
$desc = substr($desc, 0, strpos($desc,'. ', 100));
$desc = $desc."...";
return $desc;
}
}
}
$new_desc = snip();
?>

<?=$new_desc?>


And I get an error like
Warning: strpos(): Offset not contained in string. in /home/public_html/foldername/test.php on line 21 ...

The offset is present its just that calling the chunk of text to the substr() turns up blank, if I remove $desc from the substr and replace it with

$desc = substr("the long chunk of text", 0, strpos("the long chunk of text",'. ', 100));

it all works fine? with $desc all I get is the error and the ...

Any help as always will be much appreciated

dreamcatcher

1:57 pm on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi wheelie34,

You need to declare your $desc as global inside the function or pass it directly into the function:

global $desc;

or

function snip($desc)

$new_desc = snip($desc);

This link may be helpful:
[uk.php.net...]

dc

wheelie34

2:07 pm on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks DC, I did try snip($desc) and got an error as I didn't put it in the call, I just used

$new_desc = snip();

Now with snip($desc) it works perfect, thanks again

wheelie34

11:15 am on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK now that I have more than one result I get an error for the second result.

Cannot redeclare snip($desc)

I understand why but cant figure out what to change, heres my code so far.

$sql="select * from table where widget LIKE '%$widget'";
$result = mysql_query($sql, $dblink) or die("System down");
while ($newArray = mysql_fetch_array($result))
{
$num = $newArray['num'];
$title = $newArray['title'];
$desc = $newArray['widget'];
function snip($desc, $num)
{

$desc = substr($desc, 0, strpos($desc,'. ', 100));
$desc = $desc.' <font size=1>...<BR><BR>( <a href="widget.php?num='.$num.'">Further details on this widget</a> )</font>';

return $desc;

}
$widget_snippet = snip($desc, $num);
print"<font size=\"2\" face=\"Arial, Helvetica, sans-serif\">
<br><b>Widget name:</b> <a href=\"widget.php?num=$num\">$title</a>
<br><br>$widget_snippet</font>";
}

I would prefer to be able to chop the text rather than setup a new field for the snippet text, any ideas?

wheelie34

2:22 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have now managed to get it working by moving the function OUT of the loop, hopefully thats the way to do it, it does work as expected.

coopster

3:08 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That sounds correct. You can only define a function once. I find it most useful to put the functions specific to a certain script at the top of the script so I know where they exist in the event I need to modify them. If you put a function within a loop you need to determine whether or not it exists first before you define it. Best off getting it out of the loop as you mentioned.