Primele N Cuvinte Dintr-un String, excluzand tag-uri html

Ai o întrebare legată de PHP? Incercăm să îi găsim soluţie. Sau poate doar vrei să publici un cod interesant.

Moderatori: Zamolxe, Moderatori


Avatar utilizator
mihaitha
Senior Member
Mesaje: 2383
Membru din: Vin Mai 04, 2007 12:40 pm
Localitate: Sibiu
Contact:

Mesajde mihaitha » Lun Ian 21, 2008 11:25 am

Incearca o combinatie cu substr() si strip_tags().

Avatar utilizator
vectorialpx
Senior Member
Mesaje: 4832
Membru din: Mar Mar 01, 2005 9:48 am
Localitate: Bucuresti
Contact:

Mesajde vectorialpx » Lun Ian 21, 2008 12:23 pm

adica print substr(strip_tags($string), 0, $n);
not a big deal... dar, cine sa se uite prin manual?

Avatar utilizator
mihaitha
Senior Member
Mesaje: 2383
Membru din: Vin Mai 04, 2007 12:40 pm
Localitate: Sibiu
Contact:

Mesajde mihaitha » Lun Ian 21, 2008 1:50 pm

Nu te lua de el octaviane ca nu e asa de simplu cum pare: el vrea primele n cuvinte dintr-un string, numarate dupa un strip_tags, dar la afisare le vrea cu tot cu taguri. E cam complicat de facut sincronizarea, si eu unu n-am nici timp si nici chef si nici o idee la ora asta.

andreivig
PHPRomania Supporter
Mesaje: 11
Membru din: Mie Ian 25, 2006 10:41 am

Sper sa te ajute scriptul asta

Mesajde andreivig » Lun Ian 21, 2008 3:38 pm

<?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);

Louis
Average Member
Mesaje: 119
Membru din: Mie Feb 15, 2006 10:12 am
Contact:

Mesajde Louis » Mar Ian 22, 2008 9:29 am

Multumesc @andreivig pentru script, il voi incerca.

Avatar utilizator
vectorialpx
Senior Member
Mesaje: 4832
Membru din: Mar Mar 01, 2005 9:48 am
Localitate: Bucuresti
Contact:

Mesajde vectorialpx » Mar Ian 22, 2008 9:50 am

aa, nu m-am prins ca despre asta e vorba... my bad! sorry!

Louis
Average Member
Mesaje: 119
Membru din: Mie Feb 15, 2006 10:12 am
Contact:

Mesajde Louis » Mar Ian 22, 2008 8:48 pm


andreivig
PHPRomania Supporter
Mesaje: 11
Membru din: Mie Ian 25, 2006 10:41 am

Completare script

Mesajde andreivig » Mar Ian 22, 2008 11:54 pm

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));

Louis
Average Member
Mesaje: 119
Membru din: Mie Feb 15, 2006 10:12 am
Contact:

Mesajde Louis » Mie Ian 23, 2008 1:13 pm



Înapoi la “Cod PHP”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 16 vizitatori