mediengestalter.info
FAQ :: Mitgliederliste :: MGi Team

Willkommen auf dem Portal für Mediengestalter

Aktuelles Datum und Uhrzeit: Mo 29.04.2024 05:02 Benutzername: Passwort: Auto-Login

Thema: preg_match inhalt zwischen zwei tags vom 30.10.2009


Neues Thema eröffnen   Neue Antwort erstellen MGi Foren-Übersicht -> Programmierung -> preg_match inhalt zwischen zwei tags
Seite: 1, 2  Weiter
Autor Nachricht
Psilo
Threadersteller

Dabei seit: 20.02.2007
Ort: -
Alter: 35
Geschlecht: Männlich
Verfasst Fr 30.10.2009 16:58
Titel

preg_match inhalt zwischen zwei tags

Antworten mit Zitat Zum Seitenanfang

hi,
ich versuche wie folgt den inhalt zwischen den code tags einer seite auszulesen
Code:
preg_match('|<code>(.*)</code>|', $results["content"][0], $contentCODE);

das klappt auch super.. nur findet er zum teil auch z.b. sowas
Code:
<code>testestsetstest</code><code>jfkdsafjdks</code>

also er hört oft nicht beim ersten </code> auf..
wie kann ich das mithilfe des preg_match verhindern?
danke
  View user's profile Private Nachricht senden
Eistee
Administrator

Dabei seit: 31.10.2001
Ort: Grimma
Alter: 45
Geschlecht: Männlich
Verfasst Fr 30.10.2009 17:32
Titel

Antworten mit Zitat Zum Seitenanfang

Schau dir mal den regex Operator /U an für "ungreedy".
  View user's profile Private Nachricht senden Website dieses Benutzers besuchen
Anzeige
Anzeige
DEKONSTRUKTIV

Dabei seit: 22.06.2009
Ort: bln
Alter: -
Geschlecht: -
Verfasst Fr 30.10.2009 17:34
Titel

Antworten mit Zitat Zum Seitenanfang

versuchs mal mit ner ungreedy regexp.

damn. too late.


Zuletzt bearbeitet von DEKONSTRUKTIV am Fr 30.10.2009 17:34, insgesamt 1-mal bearbeitet
  View user's profile Private Nachricht senden Website dieses Benutzers besuchen
Psilo
Threadersteller

Dabei seit: 20.02.2007
Ort: -
Alter: 35
Geschlecht: Männlich
Verfasst Fr 30.10.2009 18:39
Titel

Antworten mit Zitat Zum Seitenanfang

also ich hab das jetzt mal versucht.. nur leider hab ich absolut kein peil von regular expressions
nicht mal die obige hab ich selbst gecodet..
ich habs grad wirklich länger probiert.. aber so ganz klappt das nicht
bekomm dann gar keinen inhalt mehr zurück.
Code:
preg_match("/^<code>(.*?)<\/code>/U", $results["content"][0], $contentCODE);

bitte nicht lachen.. wie gesagt.. hab gar kein peil von regexp *hu hu huu*
  View user's profile Private Nachricht senden
DerM

Dabei seit: 18.01.2009
Ort: -
Alter: 42
Geschlecht: Männlich
Verfasst Fr 30.10.2009 18:46
Titel

Antworten mit Zitat Zum Seitenanfang

Psilo hat geschrieben:
also ich hab das jetzt mal versucht.. nur leider hab ich absolut kein peil von regular expressions
nicht mal die obige hab ich selbst gecodet..
ich habs grad wirklich länger probiert.. aber so ganz klappt das nicht
bekomm dann gar keinen inhalt mehr zurück.
Code:
preg_match("/^<code>(.*?)<\/code>/U", $results["content"][0], $contentCODE);

bitte nicht lachen.. wie gesagt.. hab gar kein peil von regexp *hu hu huu*


Du hast diesen komischen Startoperanten (heißt das so) drin vor dem <code> das Zirkumflex muss raus, da es ja auch mittdendrin vorkommen kann...

So müsste es klappen.. Bin allerdings auch nicht der Echsen freak ^^
Code:
preg_match("/<code>(.*?)<\/code>/U", $results["content"][0], $contentCODE);
  View user's profile Private Nachricht senden
Psilo
Threadersteller

Dabei seit: 20.02.2007
Ort: -
Alter: 35
Geschlecht: Männlich
Verfasst Fr 30.10.2009 18:52
Titel

Antworten mit Zitat Zum Seitenanfang

hi,
leider hab ich damit das gleiche ergebnis wie mit meiner ersten regexp
also es wird nicht beim ersten </code> aufgehört

die ausgabe
Code:
<code> &lt;?php
 class&nbsp; methoden
 {
 &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode1 ()&nbsp;{
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert&nbsp; =&nbsp; methoden :: methode2 ();
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*&nbsp;oder&nbsp;*/
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert2&nbsp; =&nbsp; $this -&gt; methode2 ();
 &nbsp;&nbsp;&nbsp;&nbsp;}
 &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode2 ()&nbsp;{
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return&nbsp; 'blub' ;
 &nbsp;&nbsp;&nbsp;&nbsp;}
 }
 ?&gt; </code>
 </code>

 
 mfg
 robo47
UND NOCH SEHR SEHR VIEL MEHR

das sind schon zwei </code> tags und trotzdem macht er weiter..
das seltsame ist, dass dieser code so nur im quelltext steht.. auf der seite selbst wird das gewünschte erste code tag ausgegeben.
also
Code:
<code> &lt;?php
 class&nbsp; methoden
 {
 &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode1 ()&nbsp;{
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert&nbsp; =&nbsp; methoden :: methode2 ();
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*&nbsp;oder&nbsp;*/
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert2&nbsp; =&nbsp; $this -&gt; methode2 ();
 &nbsp;&nbsp;&nbsp;&nbsp;}
 &nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode2 ()&nbsp;{
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return&nbsp; 'blub' ;
 &nbsp;&nbsp;&nbsp;&nbsp;}
 }
 ?&gt; </code>


ideen??

ist perfomancemäßig in ordnung wenn ich den string einfach nach dem ersten </code> abschneide?
das sollte ja auch ohne preg_match gehen oder?

und noch vielen dank für eure mühe * Applaus, Applaus *


Zuletzt bearbeitet von Psilo am Fr 30.10.2009 18:55, insgesamt 2-mal bearbeitet
  View user's profile Private Nachricht senden
DEKONSTRUKTIV

Dabei seit: 22.06.2009
Ort: bln
Alter: -
Geschlecht: -
Verfasst Fr 30.10.2009 19:16
Titel

Antworten mit Zitat Zum Seitenanfang

du willst uns glauben machen, dein preg_match erfindet stringteile hinzu? ganz sicher nich.
  View user's profile Private Nachricht senden Website dieses Benutzers besuchen
Psilo
Threadersteller

Dabei seit: 20.02.2007
Ort: -
Alter: 35
Geschlecht: Männlich
Verfasst Fr 30.10.2009 20:56
Titel

Antworten mit Zitat Zum Seitenanfang

das ist ein fakt...
das projekt ist noch nich online.. aber ich seh doch den code...
und im quelltext ist einfach mehr code als dort sein sollte..

HIER DAS GANZE ARRAY
[code:1:c40d62a581]Array
(
[0] => <code> &lt;?php
class&nbsp; methoden
{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode1 ()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert&nbsp; =&nbsp; methoden :: methode2 ();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*&nbsp;oder&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert2&nbsp; =&nbsp; $this -&gt; methode2 ();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode2 ()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return&nbsp; 'blub' ;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
?&gt; </code>
</code>


mfg
robo47









__________________
robo47.net *überarbeitet* - Blog, Codeschnipsel und mehr
Email via SMTP verschicken | Dateiuploads an Formulare schicken mit php | Mit PHP Zip-Archive erstellen und entpacken


























&nbsp;

































































02.03.2005, 10:06




&nbsp;
# 3 ( permalink )









lazydog




Erfahrener Benutzer





&nbsp;




&nbsp;
Registriert seit: 02.04.2008



Beiträge: 3.941


































Statt langer Erklärungen ein Beispiel:
PHP-Code:

<code style="white-space:nowrap">
<code> &lt;?php
class&nbsp; myclass {
&nbsp;&nbsp;&nbsp;var&nbsp; $a ;
&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;function&nbsp; myclass ( $x ){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this -&gt; a&nbsp; =&nbsp; $x ;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;function&nbsp; f1 (){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp; $this -&gt; f2 ( $this -&gt; a );
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;function&nbsp; f2 ( $zahl ){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp; 2&nbsp; *&nbsp; $zahl ;
&nbsp;&nbsp;&nbsp;}
}

$cl&nbsp; =&nbsp;new&nbsp; myclass ( 77 );
$cl -&gt; f1 ();
?&gt; </code>
</code>

[1] => &lt;?php
class&nbsp; methoden
{
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode1 ()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert&nbsp; =&nbsp; methoden :: methode2 ();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*&nbsp;oder&nbsp;*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $wert2&nbsp; =&nbsp; $this -&gt; methode2 ();
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp; methode2 ()&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return&nbsp; 'blub' ;
&nbsp;&nbsp;&nbsp;&nbsp;}
}
?&gt; </code>
</code>


mfg
robo47









__________________
robo47.net *überarbeitet* - Blog, Codeschnipsel und mehr
Email via SMTP verschicken | Dateiuploads an Formulare schicken mit php | Mit PHP Zip-Archive erstellen und entpacken


























&nbsp;

































































02.03.2005, 10:06




&nbsp;
# 3 ( permalink )









lazydog




Erfahrener Benutzer





&nbsp;




&nbsp;
Registriert seit: 02.04.2008



Beiträge: 3.941


































Statt langer Erklärungen ein Beispiel:
PHP-Code:

<code style="white-space:nowrap">
<code> &lt;?php
class&nbsp; myclass {
&nbsp;&nbsp;&nbsp;var&nbsp; $a ;
&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;function&nbsp; myclass ( $x ){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this -&gt; a&nbsp; =&nbsp; $x ;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;function&nbsp; f1 (){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp; $this -&gt; f2 ( $this -&gt; a );
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;function&nbsp; f2 ( $zahl ){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp; 2&nbsp; *&nbsp; $zahl ;
&nbsp;&nbsp;&nbsp;}
}

$cl&nbsp; =&nbsp;new&nbsp; myclass ( 77 );
$cl -&gt; f1 ();
?&gt; </code>


)
<div class='content'></div><p class='original'>Quelle: <a rel='nofollow' href='http://php.de/php-tipps-2005/18818-methode-aus-einer-anderen-methode-aufrufen.html'>http://php.de/php-tipps-2005/18818-methode-aus-einer-anderen-methode-aufrufen.html</a></p><p class='more'><a href='/code/index/?url=http://php.de/php-tipps-2005/18818-methode-aus-einer-anderen-methode-aufrufen.html'>mehr zu <em>http://php.de/php-tipps-2005/18818-methode-aus-einer-anderen...</em></a></p></div><div class='balken'>&nbsp;</div><div class='website'><p class='title'>PHP: substr - Manual</p><p class='description'><strong>Beschreibung:</strong> Keine Beschreibung</p>Array
(
[0] => <code> &lt;?php $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp;- 1 );&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;gibt&nbsp;"f"&nbsp;zurück $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp;- 2 );&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;gibt&nbsp;"ef"&nbsp;zurück $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp;- 3 ,&nbsp; 1 );&nbsp; //&nbsp;gibt&nbsp;"d"&nbsp;zurück ?&gt; </code> length Ist length angegeben und positiv, enthält der zurückgegebene String höchstens length Zeichen ab start (abhängig von der Länge von string ). Ist length angegeben und negativ, werden genau so viele Zeichen vom Ende von string abgeschnitten (nachdem die Startposition ermittelt wurde, sollte start negativ sein). Gibt start eine Position hinter dem abzuschneidenden Teil an, wird ein leerer String zurückgegeben. Beispiel #2 Negativen length -Wert verwenden <code> &lt;?php $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp; 0 ,&nbsp;- 1 );&nbsp;&nbsp; //&nbsp;gibt&nbsp;"abcde"&nbsp;zurück $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp; 2 ,&nbsp;- 1 );&nbsp;&nbsp; //&nbsp;gibt&nbsp;"cde"&nbsp;zurück $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp; 4 ,&nbsp;- 4 );&nbsp;&nbsp; //&nbsp;gibt&nbsp;""&nbsp;zurück $rest&nbsp; =&nbsp; substr ( "abcdef" ,&nbsp;- 3 ,&nbsp;- 1 );&nbsp; //&nbsp;gibt&nbsp;"de"&nbsp;zurück ?&gt; </code> Rückgabewerte Gibt den extrahierten Teil der Zeichenkette zurück. Beispiele Beispiel #3 Generelle Verwendung von substr() <code> &lt;?php echo&nbsp; substr ( 'abcdef' ,&nbsp; 1 );&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;bcdef echo&nbsp; substr ( 'abcdef' ,&nbsp; 1 ,&nbsp; 3 );&nbsp;&nbsp; //&nbsp;bcd echo&nbsp; substr ( 'abcdef' ,&nbsp; 0 ,&nbsp; 4 );&nbsp;&nbsp; //&nbsp;abcd echo&nbsp; substr ( 'abcdef' ,&nbsp; 0 ,&nbsp; 8 );&nbsp;&nbsp; //&nbsp;abcdef echo&nbsp; substr ( 'abcdef' ,&nbsp;- 1 ,&nbsp; 1 );&nbsp; //&nbsp;f //&nbsp;Auf&nbsp;ein&nbsp;einzelnes&nbsp;Zeichen&nbsp;eines&nbsp;Strings&nbsp;kann&nbsp;auch&nbsp;mittels //&nbsp;eckiger&nbsp;Klammern&nbsp;zugegriffen&nbsp;werden $string&nbsp; =&nbsp; 'abcdef' ; echo&nbsp; $string [ 0 ];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;a echo&nbsp; $string [ 3 ];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;d echo&nbsp; $string [ strlen ( $string )- 1 ];&nbsp; //&nbsp;f ?&gt; </code> Siehe auch strrchr() - Sucht das letzte Vorkommen eines Zeichens in einem String substr_replace() - Ersetzt Text innerhalb einer Zeichenkette preg_match() - Führt eine Suche mit einem regulären Ausdruck durch trim() - Entfernt Whitespaces (oder andere Zeichen) am Anfang und Ende eines Strings mb_substr() - Get part of string wordwrap() - Bricht einen String nach einer bestimmten Anzahl Zeichen um trim substr_replace Last updated: Fri, 30 Oct 2009 &nbsp; add a note User Contributed Notes substr info /a/ nopticon /dot/ com 08-Oct-2009 06:52 <code> &lt;?php // Substring without losing word meaning and // tiny words (length 3 by default) are included on the result. // "..." is added if result do not reach original string length function _substr ( $str , $length , $minword = 3 ) { &nbsp;&nbsp;&nbsp; $sub = '' ; &nbsp;&nbsp;&nbsp; $len = 0 ; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; foreach ( explode ( ' ' , $str ) as $word ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $part = (( $sub != '' ) ? ' ' : '' ) . $word ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $sub .= $part ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $len += strlen ( $part ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( strlen ( $word ) &gt; $minword &amp;&amp; strlen ( $sub ) &gt;= $length ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; break; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return $sub . (( $len &lt; strlen ( $str )) ? '...' : '' ); } ?&gt; </code> kaj dot strom at kapsi dot fi 05-Oct-2009 11:42 <code> One thing to keep in mind when using string indexes and UTF-8 is that string indexes are NOT multi-byte safe. &lt;?php $string = 'äää1' ; echo $string [ 3 ]; ?&gt; Outputs: ¤ When it logically should output "1". This is not a bug, as PHP 'normal' string functions are not intended to be multi-byte safe. This can be solved by using this function &lt;?php /** &nbsp;* &nbsp;* @param string $string String to "search" from &nbsp;* @param int $index Index of the letter we want. &nbsp;* @return string The letter found on $index. &nbsp;*/ function charAt ( $string , $index ){ &nbsp;&nbsp;&nbsp; if( $index &lt; mb_strlen ( $string )){ &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return mb_substr ( $string , $index , 1 ); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; else{ &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return - 1 ; &nbsp;&nbsp;&nbsp; } } ?&gt; </code> gkhelloworld at gmail dot com 26-Jul-2009 10:44 <code> Shortens the filename and its expansion has seen. $file = "Hellothisfilehasmorethan30charactersandthisfayl.exe"; function funclongwords($file) { if (strlen($file) &gt; 30) { $vartypesf = strrchr($file,"."); $vartypesf_len = strlen($vartypesf); $word_l_w = substr($file,0,15); $word_r_w = substr($file,-15); $word_r_a = substr($word_r_w,0,-$vartypesf_len); return $word_l_w."...".$word_r_a.$vartypesf; } else return $file; } // RETURN: Hellothisfileha...andthisfayl.exe // other simples on rollyz.net </code> gkhelloworld at gmail dot com 26-Jul-2009 10:40 <code> Shortens the filename and its expansion has seen. $file = "Hellothisfilehasmorethan30charactersandthisfayl.exe"; function funclongwords($file) { if (strlen($file) &gt; 30) { $vartypesf = strrchr($file,"."); $vartypesf_len = strlen($vartypesf); $word_l_w = substr($file,0,15); $word_r_w = substr($file,-15); $word_r_a = substr($word_r_w,0,-$vartypesf_len); return $word_l_w."...".$word_r_a.$vartypesf; } else return $file; } // RETURN: Hellothisfileha...andthisfayl.exe // other simples on rollyz.net </code> gkhelloworld at gmail dot com 26-Jul-2009 10:39 <code> Shortens the filename and its expansion has seen.

&lt;?php
$file = "Hellothisfilehasmorethan30charactersandthisfayl.exe" ;

function funclongwords ( $file )
{
if ( strlen ( $file ) &gt; 30 )
{
$vartypesf = strrchr ( $file , "." );
$vartypesf_len = strlen ( $vartypesf );
$word_l_w = substr ( $file , 0 , 15 );
$word_r_w = substr ( $file ,- 15 );
$word_r_a = substr ( $word_r_w , 0 ,- $vartypesf_len );

return $word_l_w . "..." . $word_r_a . $vartypesf ;
}
else
return $file ;
}
// RETURN: Hellothisfileha...andthisfayl.exe
?&gt; </code> erez at meezoog dot com 07-Jul-2009 04:59 <code> An improvement to substrws - multi-byte, and closes tags better. /** * word-sensitive substring function with html tags awareness * @param text The text to cut * @param len The maximum length of the cut string * @returns string **/ function mb_substrws( $text, $len=180 ) { &nbsp;&nbsp;&nbsp; if( (mb_strlen($text) &gt; $len) ) { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $whitespaceposition = mb_strpos($text," ",$len)-1; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if( $whitespaceposition &gt; 0 ) { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $chars = count_chars(mb_substr($text, 0, ($whitespaceposition+1)), 1); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if ($chars[ord('&lt;')] &gt; $chars[ord('&gt;')]) &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $whitespaceposition = mb_strpos($text,"&gt;",$whitespaceposition)-1; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $text = mb_substr($text, 0, ($whitespaceposition+1)); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; // close unclosed html tags &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if( preg_match_all("|&lt;([a-zA-Z]+)|",$text,$aBuffer) ) { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( !empty($aBuffer[1]) ) { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; preg_match_all("|&lt;/([a-zA-Z]+)&gt;|",$text,$aBuffer2); &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( count($aBuffer[1]) != count($aBuffer2[1]) ) { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; foreach( $aBuffer[1] as $index =&gt; $tag ) { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( empty($aBuffer2[1][$index]) || $aBuffer2[1][$index] != $tag) &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $text .= '&lt;/'.$tag.'&gt;'; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; return $text; } </code> kaysar in ymail in com 08-Jun-2009 06:58 <code> Drop extensions of a file (even from a file location string) &lt;?php $filename = "c:/some dir/abc defg. hi.jklmn" ; echo substr ( $filename , 0 , ( strlen ( $filename )) - ( strlen ( strrchr ( $filename , '.' )))); ?&gt; output: c:/some dir/abc defg. hi Hope it may help somebody like me.. (^_^) </code> benny at bennyborn dot de 06-May-2009 08:52 <code> If you need a word-sensitive and also html-tags aware version of substr, this one should do the job. It works fine for me

&lt;?php
/**
* word-sensitive substring function with html tags awareness
* @param text The text to cut
* @param len The maximum length of the cut string
* @returns string
**/
function substrws ( $text , $len = 180 ) {

&nbsp;&nbsp;&nbsp; if( ( strlen ( $text ) &gt; $len ) ) {

&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $whitespaceposition = strpos ( $text , " " , $len )- 1 ;

&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if( $whitespaceposition &gt; 0 )
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $text = substr ( $text , 0 , ( $whitespaceposition + 1 ));

&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; // close unclosed html tags
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if( preg_match_all ( "|&lt;([a-zA-Z]+)&gt;|" , $text , $aBuffer ) ) {

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( !empty( $aBuffer [ 1 ]) ) {

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; preg_match_all ( "|&lt;/([a-zA-Z]+)&gt;|" , $text , $aBuffer2 );

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( count ( $aBuffer [ 1 ]) != count ( $aBuffer2 [ 1 ]) ) {

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; foreach( $aBuffer [ 1 ] as $index =&gt; $tag ) {

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( empty( $aBuffer2 [ 1 ][ $index ]) || $aBuffer2 [ 1 ][ $index ] != $tag )
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $text .= '&lt;/' . $tag . '&gt;' ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; return $text ;
}
?&gt; </code> Andreas Bur (andreas dot buro at gmail dot com) 29-Apr-2009 01:25 <code> For getting a substring of UTF-8 characters, I highly recommend mb_substr &lt;?php &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $utf8string = "cakeæøå" ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; echo substr ( $utf8string , 0 , 5 ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; // output cake# &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; echo mb_substr ( $utf8string , 0 , 5 , 'UTF-8' ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; //output cakeæ ?&gt; </code> webmaster at oehoeboeroe dot nl 18-Apr-2009 09:07 <code> You might expect substr('123456', 6) to return an empty string. Instead it returns boolean FALSE.

This behavior should be mentioned in the Return Values section of the manual. Instead it is only mentioned in the Parameters section.

If you need an empty string instead of a boolean FALSE you should typecast the result to a string.

&lt;?php
$a = substr ( '123456' , 6 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // equivalent to $a = FALSE
$a = (string) substr ( '123456' , 6 );&nbsp;&nbsp; // equivalent to $a = '';
?&gt; </code> Jakob S. 15-Apr-2009 02:13 <code> If you need to get the first $num Chars of $str and remove them from $str you'll want to use following function:

&lt;?php
function string_shift (&amp; $str , $num ) {
&nbsp;&nbsp;&nbsp; $cutOff = substr ( $str , 0 , $num );
&nbsp;&nbsp;&nbsp; $str = substr ( $str , $num );
&nbsp;&nbsp;&nbsp; return $cutOff ;
}
?&gt; </code> sajjad at sajjad dot biz 08-Apr-2009 07:28 <code> Substring utf-8 strings! very simple! &lt;?php function substru ( $str , $from , $len ){ &nbsp;&nbsp;&nbsp; return preg_replace ( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' . '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s' , '$1' , $str ); } ?&gt; </code> link 22-Mar-2009 01:52 <code> And as always there is bound to be a bug: &lt;?php function strlen_entities ( $text ) { &nbsp;&nbsp;&nbsp; preg_match_all ( &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '/((?:&amp;(?:#[0-9]{2,}|[a-z]{2,});)|(?:[^&amp;])|' .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '(?:&amp;(?!\w;)))s' , $text , $textarray ); &nbsp;&nbsp;&nbsp; return count ( $textarray [ 0 ]); } function substr_entities ( $text , $start , $limit = 0 ) { &nbsp;&nbsp;&nbsp; $return = '' ; &nbsp;&nbsp;&nbsp; preg_match_all ( &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '/((?:&amp;(?:#[0-9]{2,}|[a-z]{2,});)|(?:[^&amp;])|' .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '(?:&amp;(?!\w;)))s' , $text , $textarray ); &nbsp;&nbsp;&nbsp; $textarray = $textarray [ 0 ]; &nbsp;&nbsp;&nbsp; $numchars = count ( $textarray )- 1 ; &nbsp;&nbsp;&nbsp; if ( $start &gt;= $numchars ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return false ; &nbsp;&nbsp;&nbsp; if ( $start &lt; 0 ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $start = ( $numchars )+ $start + 1 ; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if ( $start &gt;= 0 ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( $limit == 0 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $end = $numchars ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; elseif ( $limit &gt; 0 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $end = $start +( $limit - 1 ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; else &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $end = ( $numchars )+ $limit ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; for ( $i = $start ;( $i &lt;= $end &amp;&amp; isset( $textarray [ $i ])); $i ++) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $return .= $textarray [ $i ]; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return $return ; &nbsp;&nbsp;&nbsp; } } ?&gt; </code> link 21-Mar-2009 03:19 <code> I created some functions for entity-safe splitting+lengthcounting: &lt;?php function strlen_entities ( $text ) { &nbsp;&nbsp;&nbsp; preg_match_all ( &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '/((?:&amp;(?:#[0-9]{2,}|[a-z]{2,});)|(?:[^&amp;])|' .&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '(?:&amp;(?!\w;)))s' , $text , $textarray ); &nbsp;&nbsp;&nbsp; return count ( $textarray [ 0 ]); }&nbsp; function substr_entities ( $text , $start , $limit = 0 ) { &nbsp;&nbsp;&nbsp; $return = '' ; &nbsp;&nbsp;&nbsp; preg_match_all ( &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '/((?:&amp;(?:#[0-9]{2,}|[a-z]{2,});)|(?:[^&amp;])|' .&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; '(?:&amp;(?!\w;)))s' , $text , $textarray ); &nbsp;&nbsp;&nbsp; $textarray = $textarray [ 0 ]; &nbsp;&nbsp;&nbsp; $numchars = count ( $textarray )- 1 ; &nbsp;&nbsp;&nbsp; if ( $start &gt;= $numchars ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return false ; &nbsp;&nbsp;&nbsp; if ( $start &lt; 0 ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $start = ( $numchars )+ $start + 1 ; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; if ( $start &gt;= 0 ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( $limit == 0 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $end = $numchars ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; elseif ( $limit &gt; 0 ) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $end = $start +( $limit - 1 ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; else &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $end = ( $numchars )+ $limit ; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; for ( $i = $start ; $i &lt;= $end ; $i ++) &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $return .= $textarray [ $i ]; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; } &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return $return ; &nbsp;&nbsp;&nbsp; } } ?&gt; </code> ralphingATNOSPAMDOTpocztaDOTonetDOTpl 11-Mar-2009 01:08 <code> this function return string between two delimiters
i found it very useful to get text between html tags

&lt;?php
function strbet ( $inputStr , $delimeterLeft , $delimeterRight , $debug = false ) {
&nbsp;&nbsp;&nbsp; $posLeft = strpos ( $inputStr , $delimeterLeft );
&nbsp;&nbsp;&nbsp; if ( $posLeft === false ) {
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( $debug ) {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; echo "Warning: left delimiter '{$delimeterLeft}' not found" ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return false ;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; $posLeft += strlen ( $delimeterLeft );
&nbsp;&nbsp;&nbsp; $posRight = strpos ( $inputStr , $delimeterRight , $posLeft );
&nbsp;&nbsp;&nbsp; if ( $posRight === false ) {
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( $debug ) {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; echo "Warning: right delimiter '{$delimeterRight}' not found" ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return false ;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; return substr ( $inputStr , $posLeft , $posRight - $posLeft );
}
?&gt; </code> jude dot dadzie at gmail dot com 24-Feb-2009 03:30 <code> Here's a simple direct way of extracting the information you need from a string...

Suppose your string is "C:/www/vhosts/example.com/images/image1.jpg" which points to an image. Let's assume you want the part "images/image1.jpg". We have to do the ff:

&lt;?php
$image = "C:/www/vhosts/example.com/images/image1.jpg"

$image = substr ( $image , strpos ( $image , 'image' , ( strlen ( $image )- strpos ( $image , 'image' ))));

echo $image . "&lt;br/&gt;" ; //will give 'images/image1.jpg'
?&gt; </code> wrapbit at yahoo dot com 31-Oct-2008 09:00 <code> &lt;?php
$cfg [ csvEnc ] = '"' ;
$cfg [ csvEsc ] = '\\' ;
$cfg [ csvTerm ] = "," ;

if( ! function_exists ( "parse_csv_aux" ) ){
&nbsp;&nbsp;&nbsp; function parse_csv_aux ( $string ){
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; global $cfg ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $product = "" ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $in_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $skipped_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; for( $i = 0 ; $i &lt; strlen ( $string ) ; $i ++){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $string { $i } == $cfg [ csvEnc ] ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $skipped_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= $cfg [ csvEnc ];
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $skipped_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else if( ! $skipped_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $skipped_quote = TRUE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $in_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $skipped_quote ) $skipped_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $in_quote = TRUE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else if( $string { $i } == "," ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= "," ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= " ~ " ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; //$in_quote = FALSE;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= $string { $i };
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= $string { $i };
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return $product ;
&nbsp;&nbsp;&nbsp; }
}

if( ! function_exists ( "parse_csv" ) ){
&nbsp;&nbsp;&nbsp; function parse_csv ( $string ){
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; global $cfg ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $data = array();
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if( is_string ( $string ) &amp;&amp; ( stripos ( $string , "\n" ) !== FALSE )&nbsp; &nbsp; ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $data = explode ( "\n" , parse_csv_aux ( $string ) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; foreach( $data as $key =&gt; $row ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $columns = array();
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; //$row = strtr(&nbsp; &nbsp; $row, array( "\";\"" =&gt; "\";\"", ";" =&gt; " ; " )&nbsp; &nbsp; );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( stripos ( $row , " ~ " ) !== FALSE ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $columns = explode ( " ~ " , $row );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( ! is_array ( $columns ) ) $columns = array( strval ( $columns ) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $data [ $key ] = $columns ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return $data ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; else if( is_string ( $string ) &amp;&amp; ( stripos ( ( $string = parse_csv_aux ( $string )), " ~ " ) !== FALSE )&nbsp; &nbsp; ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $columns = explode ( " ~ " , $string );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( ! is_array ( $columns ) ) $columns = array( strval ( $columns ) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; return array( $columns );
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; else return strval ( $string );
&nbsp;&nbsp;&nbsp; } /* end function parse_csv */
} /* end not function exists parse_csv */

if( ! function_exists ( "store_csv_aux" ) ){
&nbsp;&nbsp;&nbsp; function store_csv_aux ( $string ){
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; global $cfg ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $string = strtr ( $string , array( "\n" =&gt; "" ) );
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $product = "" ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $in_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; for( $i = 0 ; $i &lt; strlen ( $string ) ; $i ++ ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $string { $i } == $cfg [ csvEnc ] ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= "\"{$cfg[csvEnc]}" ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= "\"\"{$cfg[csvEnc]}" ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $in_quote = TRUE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else if( $string { $i } == "," ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= "," ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= "\"," ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $in_quote = TRUE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= $cfg [ csvEnc ];
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $in_quote = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= $string { $i };
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= $string { $i };
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if( $in_quote ) $product .= $cfg [ csvEnc ];
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return $product ;
&nbsp;&nbsp;&nbsp; }
}

if( ! function_exists ( "store_csv" ) ){
&nbsp;&nbsp;&nbsp; function store_csv ( $data ){
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; global $cfg ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if(! is_array ( $data ))return strval ( $data );
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $passed_rows = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $product = "" ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; foreach( $data as $row ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $passed_rows ) $product .= "\n" ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( is_array ( $row ) ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $columns = "" ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $passed_cols = FALSE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; foreach( $row as $column ){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; if( $passed_cols ) $columns .= "," ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $columns .= store_csv_aux ( $column );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $passed_cols = TRUE ;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= strval ( $columns );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $product .= strtr ( strval ( $row ), array( "\n" =&gt; "" ) );
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $passed_rows = TRUE ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; return $product ;
&nbsp;&nbsp;&nbsp; } /* end function store_csv */
} /* end not function exists store_csv */
?&gt;


[EDIT BY danbrown AT php DOT net: This is a bugfix rewrite of a function originally written by "Alexander Peev".] </code> bill at eupeople dot net 31-Oct-2008 12:52 <code> hi, really basic function to take blob with full http url's and turn then into "more info" links, handy for page layout etc ;)

&lt;?php
function urltolink ( $data ){

&nbsp;&nbsp;&nbsp; while ( strpos ( $wdata , "http" )) {

&nbsp;&nbsp;&nbsp; $op = strpos ( $wdata , "http" );
&nbsp;&nbsp;&nbsp; $rdata = substr ( $wdata , 0 , $op );
&nbsp;&nbsp;&nbsp; $ndata = substr ( $wdata , $op , strlen ( $wdata )- $op );
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; $cp = strpos ( $ndata , "\n" );
&nbsp;&nbsp;&nbsp; $link = substr ( $ndata , 0 , $cp );
&nbsp;&nbsp;&nbsp; $oc = $op + $cp ;
&nbsp;&nbsp;&nbsp; $wdata = substr ( $wdata , $oc , strlen ( $wdata )- $oc );
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; $edata = $edata . "$rdata &lt;a href=\"$link\"&gt;more info&lt;/a&gt;&lt;br /&gt;" ;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; return $edata ;
}
?&gt; </code> mar dot czapla at gmail dot com 24-Oct-2008 09:31 <code> Here we have gr8 function which simply convert ip address to a number using substr with negative offset.
You can need it if you want to compare some IP addresses converted to a numbers.
For example when using ip2country, or eliminating same range of ip addresses from your website :D

&lt;?php

function ip2no ( $val )
{&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp; list( $A , $B , $C , $D )&nbsp; &nbsp; =&nbsp; &nbsp; explode ( "." , $val );
&nbsp;&nbsp;&nbsp; return
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; substr ( "000" . $A ,- 3 ).
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; substr ( "000" . $B ,- 3 ).
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; substr ( "000" . $C ,- 3 ).
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; substr ( "000" . $D ,- 3 );
}

$min&nbsp; &nbsp; &nbsp; &nbsp; =&nbsp; &nbsp; ip2no ( "10.11.1.0" );
$max&nbsp; &nbsp; &nbsp; &nbsp; =&nbsp; &nbsp; ip2no ( "111.11.1.0" );
$visitor&nbsp; &nbsp; =&nbsp; &nbsp; ip2no ( "105.1.20.200" );

if( $min &lt; $visitor &amp;&amp; $visitor &lt; $max )&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp; {&nbsp; &nbsp; echo 'Welcome !' ;&nbsp; &nbsp; }
else&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp; {&nbsp; &nbsp; echo 'Get out of here !' ;&nbsp; &nbsp; }

?&gt; </code> mr.davin 29-Sep-2008 05:01 <code> Simple use of substr to determine possession:

&lt;?php
function possessive ( $word ) {
&nbsp;&nbsp;&nbsp; return&nbsp; $word .( substr ( $word , - 1 ) == 's' ? "'" : "'s" );
}

// Davis =&gt; Davis'
// Paul =&gt; Paul's
?&gt; </code> NULL_byte 19-Sep-2008 10:21 <code> &lt;?php function insert_substr ( $str , $pos , $substr ) { &nbsp;&nbsp;&nbsp; $part1 = substr ( $str , 0 , - $pos ); &nbsp;&nbsp;&nbsp; $part2 = substr ( $str , - $pos ); &nbsp;&nbsp;&nbsp; return $part1 . $substr . $part2 ; } ?&gt; </code> baldaris69 at yahoo dot com 29-Aug-2008 07:57 <code> ***Caution newbie***
To extract a file Extension this fuction could be useful.

&lt;?php
$file_extension = substr ( $filename , strrpos ( $filename , '. ' ) + 1 );
?&gt;


Suppose your file name is Baldaris.jpeg

strrpos will return the last dot position in the string 9 so

so the compiler will execute substr($filename , 10)

$file_extension will have value jpeg

pretty cool...

Cheer's

Baldaris </code> post [at] jannik - zappe [dot] de 05-Aug-2008 02:59 <code> Just a little function to cut a string by the wanted amount. Works in both directions. &lt;?php function cutString ( $str , $amount = 1 , $dir = "right" ) { &nbsp; if(( $n = strlen ( $str )) &gt; 0 ) &nbsp; { &nbsp;&nbsp;&nbsp; if( $dir == "right" ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp;&nbsp; $start = 0 ; &nbsp;&nbsp; &nbsp;&nbsp; $end = $n - $amount ; &nbsp;&nbsp;&nbsp; } elseif( $dir == "left" ) { &nbsp;&nbsp; &nbsp;&nbsp; $start = $amount ; &nbsp;&nbsp; &nbsp;&nbsp; $end = $n ; &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return substr ( $str , $start , $end ); &nbsp; } else return false ; } ?&gt; Enjoy ;) </code> jamesvanboxtel at wsu dot edu 31-Jul-2008 09:17 <code> Here is a quick function to get the substring of a string up to and including the last occurrence of $needle

&lt;?php
function substrtruncate ( $string , $needle )
{
&nbsp;&nbsp;&nbsp; return substr ( $string , 0 , strrpos ( $string , $needle )+ 1 );
}

$current_dir = substrtruncate ( $_SERVER [ 'SCRIPT_NAME' ], '/' );
?&gt; </code> Anonymous 29-Jul-2008 06:18 <code> I wrote this simple function to limit the middle characters of a string to a specified length.

&lt;?php
$input = "hello world"
echo( limitchrmid ( $imput , 10 )) // hel ... rld

//limit chars middle
function limitchrmid ( $value , $lenght ){
&nbsp;&nbsp;&nbsp; if ( strlen ( $value ) &gt;= $lenght ){
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $lenght_max = ( $lenght / 2 )- 3 ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $start = strlen ( $value )- $lenght_max ;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $limited = substr ( $value , 0 , $lenght_max );
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $limited .= " ... " ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $limited .= substr ( $value , $start , $lenght_max );
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else{
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $limited = $value ;
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; return $limited ;
}
?&gt; </code> svihel 27-Jun-2008 08:09 <code> joao dot martins at plako dot net
26-Mar-2008 09:14

ben at enemy dot dk
10-Feb-2008 05:48

Updated function. The previous one will return empty value if the $string has no letter spaces. This is usefull if some of your strings have only one word.

&lt;?php
function cutText ( $string , $setlength ) {
&nbsp;&nbsp;&nbsp; $length = $setlength ;
&nbsp;&nbsp;&nbsp; if( $length &lt; strlen ( $string )){
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; while (( $string { $length } != " " ) AND ( $length &gt; 0 )) {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; $length --;
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( $length == 0 ) return substr ( $string , 0 , $setlength );
&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; else return substr ( $string , 0 , $length );
&nbsp;&nbsp;&nbsp; }else return $string ;
}
?&gt; </code> serzh at nm dot ru 03-Jun-2008 10:13 <code> easy and quick way to limit length of a text by not cutting full words:

textLimit('some words', 7) is 'some...'

&lt;?php
function textLimit ( $string , $length , $replacer = '...' )
{
&nbsp; if( strlen ( $string ) &gt; $length )
&nbsp; return ( preg_match ( '/^(.*)\W.*$/' , substr ( $string , 0 , $length + 1 ), $matches ) ? $matches [ 1 ] : substr ( $string , 0 , $length )) . $replacer ;
&nbsp;
&nbsp; return $string ;
}
?&gt; </code> Anonymous 17-Mar-2008 11:53 <code> Split a string to an array of strings specified by an array of lengths: &lt;?php function split_by_lengths ( $inString , $arrayLengths ) { &nbsp;&nbsp;&nbsp; $output = array(); &nbsp;&nbsp;&nbsp; foreach ( $arrayLengths as $oneLength ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $output [] = substr ( $inString , 0 , $oneLength ); &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; $inString = substr ( $inString , $oneLength ); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; return ( $output ); } ?&gt; split_by_lengths('teststringtestteststring', array(4,6,4,4,6)) returns: array('test','string','test','test','string') Don't use it on user input without some error handling! </code> kriskra at gmail dot com 01-Mar-2008 01:21 <code> The javascript charAt equivalent in php of felipe has a little bug. It's necessary to compare the type (implicit) aswell or the function returns a wrong result: &lt;?php function charAt ( $str , $pos ) { &nbsp;&nbsp;&nbsp; return ( substr ( $str , $pos , 1 ) !== false ) ? substr ( $str , $pos , 1 ) : - 1 ; } ?&gt; </code> Anonymous 23-Feb-2008 01:12 <code> I've used the between, after, before, etc functions that biohazard put together for years and they work great.&nbsp; I've also added to it a new function that I use a lot and thought others might like it as well.&nbsp; It uses his before/after functions so they are required to use it.

&lt;?php
$example_html = "&lt;p&gt;test1 Test2&lt;/p&gt;&lt;title&gt;hi there&lt;/title&gt;&lt;p&gt;Testing&lt;/p&gt;" ;
$paragraph_text = multi_between ( '&lt;p&gt;' , '&lt;/p&gt;' , $example_html );

//Prints an arry of:
//Array ( [1] =&gt; test1 Test2 [2] =&gt; Testing )
print_r ( $paragraph_text );

function multi_between ( $this , $that , $inthat )
{
&nbsp;&nbsp; $counter = 0 ;
&nbsp;&nbsp; while ( $inthat )
&nbsp;&nbsp; {
&nbsp;&nbsp; &nbsp;&nbsp; $counter ++;
&nbsp;&nbsp; &nbsp;&nbsp; $elements [ $counter ] = before ( $that , $inthat );
&nbsp;&nbsp; &nbsp;&nbsp; $elements [ $counter ] = after ( $this , $elements [ $counter ]);
&nbsp;&nbsp; &nbsp;&nbsp; $inthat = after ( $that , $inthat );
&nbsp;&nbsp; }
&nbsp;&nbsp; return $elements ;
}
//Get the help functions from biohazard's post below.
?&gt; </code> highstrike at gmail dot com 06-Jan-2008 04:47 <code> Because i didnt see a function that would cut a phrase from a text (article or whatever) no matter where, front/middle/end and add ... + keeping the words intact, i wrote this: Usage: - The parameter $value if array will need the whole text and the portion you want to start from, a string. EG: cuttext(array($text, $string), 20). If the string is "have" and is near the beginning of the text, the function will cut like "I have a car ...", if the string is in the middle somewhere it will cut like "... if you want to have your own car ..." and if its somewhere near the end it will cut like "... and you will have one." - The $length parameter is self explanatory. Note: if you have just a string "127hh43h2h52312453jfks2" and you want to cut it, just use the function like so: cuttext($string, 10) and it will cut it like "127hh43h2h..." &lt;?php //////////////////////////////////////////////////////// // Function:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; cuttext // Description: Cuts a string and adds ... function cuttext ( $value , $length ) {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; if( is_array ( $value )) list( $string , $match_to ) = $value ; &nbsp;&nbsp;&nbsp; else { $string = $value ; $match_to = $value { 0 }; } &nbsp;&nbsp;&nbsp; $match_start = stristr ( $string , $match_to ); &nbsp;&nbsp;&nbsp; $match_compute = strlen ( $string ) - strlen ( $match_start ); &nbsp;&nbsp;&nbsp; if ( strlen ( $string ) &gt; $length ) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; if ( $match_compute &l


Zuletzt bearbeitet von Psilo am Fr 30.10.2009 21:05, insgesamt 1-mal bearbeitet
  View user's profile Private Nachricht senden
 
Ähnliche Themen Zwei Selectboxen, Javascript und wechselnder Inhalt
[javascript] replace() ersetzt nur einen match
Zwei <Forms>, Zwei Checkboxen und ein Problem
Frage zu div-tags
PHP Tags ersetzen
Was ist so toll an DIV-Tags?
Neues Thema eröffnen   Neue Antwort erstellen Seite: 1, 2  Weiter
MGi Foren-Übersicht -> Programmierung


Du kannst keine Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum nicht antworten.
Du kannst an Umfragen in diesem Forum nicht mitmachen.