Primele N Cuvinte Dintr-un String, excluzand tag-uri html
Moderatori: Zamolxe, Moderatori
- vectorialpx
- Senior Member
- Mesaje: 4832
- Membru din: Mar Mar 01, 2005 9:48 am
- Localitate: Bucuresti
- Contact:
Sper sa te ajute scriptul asta
<?php
// input string
$s_input = 'Ana are mere si <a href="#"><img src="images/..." alt="asdfdfdsa" align="left" />un link</a> nu stiu cine are pere.';
$s_output = null;
//array where we store de HTML tags
$a_matches = array();
//array where we store the words
$a_words = array();
// here we just initialize some variables
$s_word = null;
$i_wordnumber = 6;
$i_key = null;
$s_value = null;
$i_pos = 0;
// --get all html tags from input strings
preg_match_all('/\<[a-zA-Z0-9\/\=\#" "\."]*\>/', $s_input, $a_matches);
$a_matches = $a_matches[0];
// --parse input
while(list($i_key, $s_value) = each($a_matches)){
//-- get the strings between html tags
$substr = substr($s_input, $i_pos, strpos($s_input, $s_value)-$i_pos);
//-- set new position in string
$i_pos = strpos($s_input, $s_value) + strlen($s_value);
//-- get the word fromn the substring
$a_words = explode(" ", $substr);
//-- parse the words
foreach($a_words as $s_word){
if ($s_word != ""){
if ($i_wordnumber > 0){
$s_output.=" ".$s_word;
$i_wordnumber--;
}else{
break(2);
}
}
}
$s_output.=$s_value;
}
var_dump($s_output);
// input string
$s_input = 'Ana are mere si <a href="#"><img src="images/..." alt="asdfdfdsa" align="left" />un link</a> nu stiu cine are pere.';
$s_output = null;
//array where we store de HTML tags
$a_matches = array();
//array where we store the words
$a_words = array();
// here we just initialize some variables
$s_word = null;
$i_wordnumber = 6;
$i_key = null;
$s_value = null;
$i_pos = 0;
// --get all html tags from input strings
preg_match_all('/\<[a-zA-Z0-9\/\=\#" "\."]*\>/', $s_input, $a_matches);
$a_matches = $a_matches[0];
// --parse input
while(list($i_key, $s_value) = each($a_matches)){
//-- get the strings between html tags
$substr = substr($s_input, $i_pos, strpos($s_input, $s_value)-$i_pos);
//-- set new position in string
$i_pos = strpos($s_input, $s_value) + strlen($s_value);
//-- get the word fromn the substring
$a_words = explode(" ", $substr);
//-- parse the words
foreach($a_words as $s_word){
if ($s_word != ""){
if ($i_wordnumber > 0){
$s_output.=" ".$s_word;
$i_wordnumber--;
}else{
break(2);
}
}
}
$s_output.=$s_value;
}
var_dump($s_output);
- vectorialpx
- Senior Member
- Mesaje: 4832
- Membru din: Mar Mar 01, 2005 9:48 am
- Localitate: Bucuresti
- Contact:
Completare script
Scriptul pe care l-am postat parseaza cuvintele doar pana la ultimul tag de html
aici e varianta completa:
<?php
// input string
$s_input = 'Ana are mere si <a href="#"><img src="images/..." alt="asdfdfdsa" align="left" />un link</a> nu stiu cine are pere.';
$s_output = null;
//array where we store de HTML tags
$a_matches = array();
//array where we store the words
$a_words = array();
// here we just initialize some variables
$s_word = null;
$i_wordnumber = 8;
$i_key = null;
$s_value = null;
$i_pos = 0;
// --get all html tags from input strings
preg_match_all('/\<[a-zA-Z0-9\/\=\#" "\."]*\>/', $s_input, $a_matches);
$a_matches = $a_matches[0];
// --parse input
while(list($i_key, $s_value) = each($a_matches)){
//-- get the strings between html tags
$substr = substr($s_input, $i_pos, strpos($s_input, $s_value)-$i_pos);
//-- set new position in string
$i_pos = strpos($s_input, $s_value) + strlen($s_value);
//-- get the word fromn the substring
$a_words = explode(" ", $substr);
//-- parse the words
foreach($a_words as $s_word){
if ($s_word != ""){
if ($i_wordnumber > 0){
$s_output.=" ".$s_word;
$i_wordnumber--;
}else{
break(2);
}
}
}
$s_output.=$s_value;
}
$substr = substr($s_input, $i_pos, strlen($s_input));
//-- get the word fromn the substring
$a_words = explode(" ", $substr);
//-- parse the words
foreach($a_words as $s_word){
if ($s_word != ""){
if ($i_wordnumber > 0){
$s_output.=" ".$s_word;
$i_wordnumber--;
}else{
break;
}
}
}
echo(htmlentities($s_output));
aici e varianta completa:
<?php
// input string
$s_input = 'Ana are mere si <a href="#"><img src="images/..." alt="asdfdfdsa" align="left" />un link</a> nu stiu cine are pere.';
$s_output = null;
//array where we store de HTML tags
$a_matches = array();
//array where we store the words
$a_words = array();
// here we just initialize some variables
$s_word = null;
$i_wordnumber = 8;
$i_key = null;
$s_value = null;
$i_pos = 0;
// --get all html tags from input strings
preg_match_all('/\<[a-zA-Z0-9\/\=\#" "\."]*\>/', $s_input, $a_matches);
$a_matches = $a_matches[0];
// --parse input
while(list($i_key, $s_value) = each($a_matches)){
//-- get the strings between html tags
$substr = substr($s_input, $i_pos, strpos($s_input, $s_value)-$i_pos);
//-- set new position in string
$i_pos = strpos($s_input, $s_value) + strlen($s_value);
//-- get the word fromn the substring
$a_words = explode(" ", $substr);
//-- parse the words
foreach($a_words as $s_word){
if ($s_word != ""){
if ($i_wordnumber > 0){
$s_output.=" ".$s_word;
$i_wordnumber--;
}else{
break(2);
}
}
}
$s_output.=$s_value;
}
$substr = substr($s_input, $i_pos, strlen($s_input));
//-- get the word fromn the substring
$a_words = explode(" ", $substr);
//-- parse the words
foreach($a_words as $s_word){
if ($s_word != ""){
if ($i_wordnumber > 0){
$s_output.=" ".$s_word;
$i_wordnumber--;
}else{
break;
}
}
}
echo(htmlentities($s_output));
Cine este conectat
Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 15 vizitatori