String parsing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Slacker
Forum Newbie
Posts: 17
Joined: Thu Aug 12, 2004 9:19 pm

String parsing

Post by Slacker »

I've been trying to parse a string:

string = blah blah blah 'html/php code here' blah 'random code' blah blah.

I want nothing in the string except for the letters, numbers, spaces. I want all code stripped. I've been trying the strip_tags with success.

Next, I want to split the string into an array. I've been using explode. Some times it works, sometimes it doesn't. Any suggestions on how to do the above?

I don't need the string parsed into an array. All I need is the string to lack all outside code and just have the letters/numbers/spaces. Then I need to be able to echo the string line by line with a max of roughly 70 characters per line. I need some wordwrapping functionality so that it won't cut a word off halfway done with it. (IE: wo on one line and rd on the next.)

here is what I have so far with varied success:

$pictext = $output . $advertise;
$pictext = trim($pictext);
$pictext = strip_tags($pictext);
$rquote = explode("\n", wordwrap($pictext, 75, "<br />"));
$totlines = count($rquote);

from here I just use a while loop to echo the lines in the array. Sometimes it works right.

Then sometimes I get:

lost words
fragmented words
it doesn't break a line up at 75 chars, but more like 150.
parts of the <br /> display.
and other odd things.

Any help appreciated.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post an actual example of this "string" ...

outside of seeing some examples.. using a few regular expressions should handle most of it.
Slacker
Forum Newbie
Posts: 17
Joined: Thu Aug 12, 2004 9:19 pm

Post by Slacker »

What I did post is pretty much what it looks like. I have the text stored in a mysql db. I know that the only random html code in the strings are for carriage returns and new lines. \r, \n
Slacker
Forum Newbie
Posts: 17
Joined: Thu Aug 12, 2004 9:19 pm

Post by Slacker »

So.. any takers?
Slacker
Forum Newbie
Posts: 17
Joined: Thu Aug 12, 2004 9:19 pm

Post by Slacker »

I ended up going with a wordwrap function. Not what I wanted, but it was all I could figure out.
Post Reply