Processing of lines in RNR
One of most often meeting problems{tasks} in programming is processing of symbolical sequences. If it is easier - than lines. As it is done{made} in language of hypertext preprocessor RNR and there is a subject of this clause{article}.
Whence and how it is possible to receive simvol`nujuposledovatel`nost`? In itself prostomsluchae - to appropriate{give} its{her} necessary variable. Other variants - to receive from the form iliiz a file. If you prisvaivaeteperemennoj the necessary value it, kakpravilo, does not require processing, takkak the programmer does{makes} assignment vkode programs, and certainly, in the necessary kind. But if the line is read out from a file, ilipoluchaetsja by means of the form, she (simvol`najastroka) requires processing.
The very first, that it is necessary to make, it to remove repeating blanks. Dljaehtogo in RNR there is a special function: chop (str);
Example of its{her} recording:
$str = chop ($str);
In the result, processed znacheniestroki $str will not be soderzhat`povtorjajuhhikhsja blanks. If nuzhnoubedit`sja that a line not soderzhitprobelov in the beginning and in the end, primenjaetsjafunkcija trim (str); ($str = trim ($str);) . Kogdatrebuetsja to remove blanks only all over again lines, it is necessary to use ltrim. Iesli speech about the beginning of a line has gone, let's be convinced, that the first simvolzaglavnyj. What to make his those, apply ucfirst (str); there Is also a function dljaperevoda in all words in line ikhpervykh letters in header - ucwords (str);. Krometogo, very much often it happens neobkhodimosravnit` a line with some pattern. A special case - search in line (about nemneskol`ko later). But no nikakojgarantii, that the received line vvedenapol`zovatelem or is received from a file vsootvetstvii with rules of spelling. In other words - a line mozhetsoderzhat` in the middle of a word ilipredlozhenija alternating header ipropisnye symbols. The decision dannojproblemy - in application of functions strtolower (str); and strtoupper (st);. These functions, accordingly, translate symbolical lines in bottom iverkhnij the register. The combination dannykhvozmozhnostej language RNR results kkorrektnomu to construction of a line irrespective of the fact how she vvedenaili has been received in an initial kind.
One more necessary thing at job sostrokami - their scrap. Often onaprimenjaetsja at processing forms dljavvoda the data. Present, that who - nibud`iz your "well-wishers" will enter in Vashugostevuju the book the text of this clause{article}. Represent, what will turn out? dljaehtogo also it is necessary to limit kolichestvovvodimykh symbols in any field of the form. As it is done{made} very simply. For the beginning, it is necessary to register ogranicheniev to the form:
<input maxlength = "100" name = "form">
Now the form with a name form is limited vkolichestve entered symbols to number 100. But it is still far from being all. Put in that, chtoobojti such restriction is very simple, inuzhno it more likely that bypokazat` to the visitor a limit of restriction. It is necessary to use funkciejRNR $form =substr ($form, 0,99) further;. It you prostootrezaete the part of the received line exceeding 100 symbols (there is figure 99 as the bill of symbols begins with zero) .Teper` all attempts of yours znakomykhzavalit` you the information will be vain, as your clever script not propustitbol`she certain{determined} by you kolichestvasimvolov.
As a matter of fact, at function substr (string, start, length); absolutely other applicability. Onavozvrahhaet a part of a line string, determined in the parameters start (beginning) ilength (length). If parameter startpolozhitel`nyj returned strokabudet to begin with start-ogo simvolastroki string.
Examples:
$form = substr ("abcdef", 1); // will return "bcdef"
$form = substr ("abcdef", 1, 3); // will return "bcd"
If the parameter start negative, tovozvrahhaemaja a line will be nachinat`sjastart-ogo a symbol from the end of a line string.
Examples:
$rest = substr ("abcdef",-1); // will return "f"
$rest = substr ("abcdef",-2); // will return "ef"
$rest = substr ("abcdef",-3, 1); // will return "d"
If the parameter length is specified and onpolozhitel`nyj, returned strokazakonchitsja for length symbols from the beginning start. It will lead to to a line with otricatel`nojdlinoj (because the beginning will be zakoncom lines), therefore vozvrahhaemajastroka will contain one symbol otnachala lines start. If length it is specified and onotricatel`nyj, returned strokazakonchitsja for length from the end of a line string. It again will lead to to a line sotricatel`noj in the length, poehtomuvozvrahhaemaja the line will be soderzhat`odin a symbol from the beginning of a line start.
Examples:
$rest = substr ("abcdef",-1,-1); // will return "bcde"
Such useful function. Except for it{her}, at data processing the form very much vazhnoumet` to cut out from received strokilishnie or simply invalid symbols. It is possible in this case primenit`special`nuju function, which zamenjaetvse ocurrences of a line needle in line haystackna the specified line str. This funkcijazapisyvaetsja so: str_replace (needle, str, haystack); .Esli it are not required to you prichudlivyepravila replacements, to you follows vsegdaispol`zovat` this function instead of ereg_replace ().
Examples:
$str = str_replace ("", \n ", $str); // the symbol of input is cut out.
$str = str_replace ("red", "black", $str); // vstroke black color will be replaced nakrasnyj.
The following important and useful function a presence{finding} lines are long. Syntax-strlen (string str); the Example - $a = strlen ("qwerty");. Vperemennoj $a there will be number 6, so kakdlinna lines - 6 symbols. Still odnainteresnaja an opportunity of language RNR text translation (so - and simvol`nykhstrok) from one coding in another. EHtoochen` it is useful, if it is necessary soglasovat`kodirovki, for example, a site and pochtovojprogrammy. And are supported samyerasprostranennye Russian codings: stringconvert_cyr_string (str, from, to);. Arguments from and tojavljajutsja one symbol, kotoryjopredeljaet initial and target kodovujutablicu. Supported types:
k - koi8-r
w - windows-1251
i - iso8859-5
a - x-cp866
d - x-cp866
m - x-mac-cyrillic
Htmlspecialchars - translates special`nyesimvoly in codes HTML. Htmlspecialchars (string); the Certain symbols have osoboeznachenie in HTML and should be zamenenykodami HTML if those they have. EHtafunkcija returns lines sproizvedennymi such changes. Function is useful for otchistkipoluchennogo from the user of the text otrazmetki HTML (boards of messages, gostevyeknigi). The following replacements are carried out:
'And' (ampersand) becomes ' *amp; '
'"' (double inverted commas) becomes ' *quot; '
' <' (the sign less) becomes ' *lt; '
'> ' (the sign more) becomes ' *gt; '
It is necessary to note, that this function nezamenjaet anything, except for mentioned above. To full processing apply funkcijuhtmlentities (). She translates all vozmozhnyesimvoly in codes HTML. Htmlentities (string); EHtafunkcija it is identical htmlspecialchars (), besides that all symbols which imejutsootvetstvujuhhij code HTML, are replaced naehtot HTML a code. In the present vremjaprimenjaetsja code table ISO-8859-1.
Separately it is necessary to consider funkciipoiska in line. Strchr - Finds pervoepojavlenie a symbol. strchr (haystack, needle); EHtafunkcija is a pseudonym dljafunkcii strstr (), and completely to her it is identical. Returns all haystack from the first pojavlenijastroki needle and up to the end. If the parameter needlene is found, comes back false. Esliparametr needle is not line, onperevoditsja in an integer irassmatrivaetsja as numerical znacheniesimvola. Strrpos - Finds pozicijuposlednego occurrences of a symbol in line. Strrpos (haystack, needle); Returns number{room} poziciiposlednego occurrences of a symbol needle vstroke haystack. It is necessary to note, that needle vehtom a case can be tol`koedinstvennym a symbol. If in kachestveparametra needle the line is underlined, totol`ko the first symbol will be used. If needle it is not found, comes back false. If the parameter needle is not line he is translated in decimal number irassmatrivaetsja as numerical znacheniesimvola. Strrchr - Finds posledneepojavlenie a symbol in line.
Strrchr (haystack, needle); This function vozvrahhaetpoziciju haystack, with which nachinaetsjaposlednee occurrence needle and prodolzhaetsjado the end haystack. Returns false if needle nenajdena. If parameter needle soderzhitbolee than one symbol, ispol`zuetsjapervyj a symbol. If the parameter needle nejavljaetsja and is considered{examined} by line he is translated vceloe number kakchislovoe value of a symbol.
Example:
// Reception of all after last novojstroki
$text = " Line 1\nLine 2\nLine 3 ";
$last = substr (strrchr ($text, 10), 1);
Strtok - breaks a line. strtok (arg1, arg2); It is used for splitting a line. EHtoznachit, that if you have a line such as " How well to program " vymozhete to break this line on otdel`nyeslova, using a blank in kachestverazdelitelja.
$string = " As it is good to program ";
$tok = strtok ($string, " ");
while ($tok) {echo " Word = $ tok <br> "; $tok = strtok (" ");}
It is necessary to note, that only pervyjvyzov functions strtok uses strokovyjargument. For everyone posledujuhhegovyzova for function strtok it is necessary tol`korazdelitel`, as it pozvoljaetkontrolirovat` position in tekuhhejstroke. For the beginning anew or dljarazbitija a new line to you neobkhodimoprosto to call strtok with parameter strokiopjat` for its{her} initialization. You mozhetevstavljat` some separators vparametr a separator. A line budetrazdeljat`sja at detection of any izukazannykh symbols. Also bud`tevnimatel`ny to separators equal "0". It can call a mistake vopredelennykh expressions.
That is required to you for job sostrokami first of all. Here privedendaleko not the full list vsekhstrokovykh functions. The most basic are considered only.

|