Closed Thread Icon

Preserved Topic: Another PHP issue, finding patterns in strings? (again?) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21014" title="Pages that link to Preserved Topic: Another PHP issue, finding patterns in strings? (again?)" rel="nofollow" >Preserved Topic: Another PHP issue, finding patterns in strings? (again?)\

 
Author Thread
DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 02-28-2002 14:32

OK, my problem here is that I don't even know where to start, or what threads to be looking at here in this forum. I know maX has spoken at some length before about regex (or something like that), but I look at the examples in the PHP docs and can't even undertand *how* they build these comparison strings. Like this, for example from the preg_replace() example...

The string "'<[\/\!]*?[^<>]*?>'si" will somehow equal every HTML tag! Which bits of that ugly string are the things it's searching for, and which are operators to build the search parameters? I feel like a primitive tribesman looking at differential calculus for the first time, which are the numbers, and which the operands? Any clue or pointer to a basic tute would be most appreciated, all I want to learn is the syntax.

I've built this not-so-elegant page with two functions, one for adding <br>'s to a page of text, and another for stripping them out (while leaving the \r\n's for display in a form). The addBRs() function, I know I could build a neater way of stripping out all of the additional CR's, but for now I'm just doing "brute force" to go through a bunch of times to get most of them.

<?php

if ($HTTP_POST_VARS['crapola'] != null) {
$crapola = $HTTP_POST_VARS['crapola']; }
else { $crapola .= "nothing, what?"; }

function stripBRs($string) {
$tempString = trim(stripslashes($string));
$searchNoBR = array("[\r\n<br>\r\n]","[\r\n<br><br>\r\n]");
$replaceNoBR = array("\r\n","\r\n\r\n");
return preg_replace ($searchNoBR, $replaceNoBR, $tempString);
}

function addBRs($string) {
$tempString = trim(stripslashes($string));
$searchBR = array(
"'([\r\n])[\s]'",
"[\r\n<br>\r\n\r\n<br>\r\n]",
"[\r\n<br><br>\r\n\r\n<br>\r\n]",
"[\r\n<br><br>\r\n\r\n<br><br>\r\n]",
"[\r\n<br><br>\r\n\r\n<br><br>\r\n]",
"[\r\n<br><br>\r\n\r\n<br><br>\r\n]",
"[\r\n<br><br>\r\n\r\n<br><br>\r\n]",
"[\r\n<br><br>\r\n\r\n<br><br>\r\n]"
);
$replaceBR = array(
"\r\n<br>\r\n",
"\r\n<br><br>\r\n",
"\r\n<br><br>\r\n",
"\r\n<br><br>\r\n",
"\r\n<br><br>\r\n",
"\r\n<br><br>\r\n",
"\r\n<br><br>\r\n",
"\r\n<br><br>\r\n"
);
return preg_replace ($searchBR, $replaceBR, $tempString);
}

?>

<form name="updateFile" action="test.pcgi" method="post">
<textarea cols=80 rows=8 name="crapola2"><?=addBRs($crapola);?></textarea><br>
<textarea cols=80 rows=8 name="crapola"><?=stripBRs(addBRs($crapola))?></textarea><br>
<input type="submit">
</form>

<?=addBRs($crapola);?>

This page lives at http://ozoniclabs.com/v2/test.pcgi - I'm hoping to use these functions as the start to my web based editing system, so people can modify their own content on a website. If you want to see what this is loike in action, see http://ozoniclabs.com/v2/v2b/ - if you click just under the copyright line, there's a 12px space that is a link to the /edit/ function, play around with it, it will only affect the content on this isolated version of the web.

Any thoughts you want to share on a project like this? Help offered, etc..., totally appreciated! :-) Once I get this module finished, I'm planning to make a version like this available, sans logo, that visitors can play with as a demonstration. (Security flaws with this, I need to strip PHP code out of any inserted text, yep! Perhaps javascript also, <iframes> whatever else, I dunno.

Your pal, -doc-

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 02-28-2002 16:12

Doc, I have written a full HTML editor, didn't bother myself with the<br>s and the new lines ....


I have made something like that, and it was a bit tricky! I had to use JS with PHP in order to make it compatible with browsers that doesn't support tags within <textarea>

image this:

<textarea>
<html>
<body>
dasdasd</body></html>

I believe NS would go crazy when he sees this, so here's the trick i used:

code:
<form name='myform' onsubmit='func1()'>
<textarea name='editfield'>
</textarea>
<input type='hidden' name='mem'>
</form>

<script>
function func1()
{
v = document.myform.mem.value =escape(document.myform.editfield.value);
}
</script>



then to load this back properly, i add an onload event like:

onload='myform.editfield.value=unescape(myform.mem.value)'

that will come handy if you want a full html editing support.
to load the file use this:
<?
$fp = fopen($fn, 'r');
$str = rawurlencode(fread($fp, filesize($fn)));
fclose($fp);
echo '<script>jsvar="$str"</script>';
?>
to write the file again:
$fp = fopen($fullfilename, 'w');
$str = urldecode($mem);
fwrite($fp, $str);
fclose($fp);

good luck,

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 02-28-2002 16:48

Yah, the thing is, I want them to come in and paste their text content into this window, then translate it out automagically as HTML for display on the website. It actually works right now, and if I can finish all the basic elements I may make my own included "ozone" code to triger a wizard for including images and such. It'll be a nifty thing to have, and should be good for doing business sites, no?


Your pal, -doc-

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 02-28-2002 18:23

I'm afraid I don't quite understand what you're trying to do, but here's a quick explanation of regular expressions for you... normally, this is covered in a chapter of a book - 30 pages or so, I assume.

In Perl, at least, regular expressions are contained within / and / instead of quotes. So

/hi there/

is a regular expression.

Backslashes are used just like in javascript or whatnot, to escape the following character.

now it gets tricky (and cool): say you want to allow my name ("john") but with or without the H. That can be expressed as

/Joh?n/

The '?' means that the preceding character can appear either zero or one times.
Similarly, a '*' means that the preceding character can appear zero or one or *more* times, so

/Joh*n/

matches Jon, John, Johhn, Johhhhhhhhn, etc.

The '+' character is just like the '*' character, except that the preceding character has to match at least once, so

/Joh+n/

will match John and Johhhhn, but not Jon. There's a way to specify "this must be there between 4 and 9 times", but I forget how off the top of my head.

Then there are the cool generic characters, such as \w, which matches any "word" character (letters, numbers, and underscores), \s, which matches any whitespace character (space, tabs, newlines, etc), and their capital counterparts - \W matches any *non* word character, and \S matches any *non* whitespace character. There are others like these.

You can create your own generic characters with brackets ([]). For instance:

/[Jj]ohn/

will match John or john.

/J[aeiou]hn/

will match John, Jahn, Jehn, Jihn, or Juhn. You can also use the ^ character at the beginning of the brackets to match any character *not* in the list:

/Jo[^qwerty]n/

will match John and Jobn and Jo3n, but not Joqn, Jown, etc.

Note that you can put *, ?, and + charcters after these, just as though they were single characters.

One last thing (and I'm skipping all but the basics): the

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-28-2002 19:54

Hey Doc,

you may want to look at the documentation and particularly the user comments for the nl2br function.

I'm kinda confused on the use of array's here. (I understand what you're doing just not the array parts.





:[ Computers let you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila. ]:

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 02-28-2002 22:07

Doc, what you're doing is very nice, but I believe it can get either complicated or limited (ie. not compatible with all browsers).

did you take a look at lime ? (from www.q42.net)



[This message has been edited by bitdamaged (edited 02-28-2002).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 02-28-2002 22:54

*just fixed the link*

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 02-28-2002 23:28

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 03-01-2002 08:53

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Lurch
Paranoid (IV) Inmate

From: Behind the Wheel
Insane since: Jan 2002

posted posted 03-01-2002 11:28

thanks for the explanation Slime
very easy for even me to understand and very helpful

--Lurch--

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 03-01-2002 12:19

if you really like REs , try playing there:
http://kameelah.org/lgwm/tools/pppad.php

example:
re = /j/i
text= John

press 'via JavaScript'

etc....

DocOzone
Maniac (V) Lord Mad Scientist
Sovereign of all the lands Ozone and just beyond that little green line over there...

From: Stockholm, Sweden
Insane since: Mar 1994

posted posted 03-01-2002 12:39

John Haggerty! My man, what a perfect mini-explanation of what those silly strings of parameters mean. I can work out the rest for myself, but I was missing the basic starting point, I think I'll write your post to a text file and save it for future reference. You are the master of short, sweet explanations!

Your pal, -doc-

« BackwardsOnwards »

Show Forum Drop Down Menu