Skip to content
Extraits de code Groupes Projets
Valider e515ab25 rédigé par cerdic's avatar cerdic Validation de marcimat
Parcourir les fichiers

feat: `extraire_balises()` prend une option `nb_max` pour limiter la recherche...

feat: `extraire_balises()` prend une option `nb_max` pour limiter la recherche en nombre d'occurences, utilisée par `extraire_balise()`
parent 78c10b40
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -2450,7 +2450,7 @@ function tags2dcsubject($tags) { ...@@ -2450,7 +2450,7 @@ function tags2dcsubject($tags) {
* - Tableau de résultats, si tableau en entrée. * - Tableau de résultats, si tableau en entrée.
**/ **/
function extraire_balise($texte, $tag = 'a') { function extraire_balise($texte, $tag = 'a') {
$balises = extraire_balises($texte, $tag); $balises = extraire_balises($texte, $tag, ['nb_max' => 1]);
if (is_array($texte)) { if (is_array($texte)) {
return array_map(function(array $a) {return (empty($a) ? '' : reset($a));}, $balises); return array_map(function(array $a) {return (empty($a) ? '' : reset($a));}, $balises);
} }
...@@ -2478,11 +2478,13 @@ function extraire_balise($texte, $tag = 'a') { ...@@ -2478,11 +2478,13 @@ function extraire_balise($texte, $tag = 'a') {
* texte(s) dont on souhaite extraire une balise html * texte(s) dont on souhaite extraire une balise html
* @param string $tag * @param string $tag
* Nom de la balise html à extraire * Nom de la balise html à extraire
* @param array $options
* int @nb_max : nombre d'occurence maxi à extraire
* @return array * @return array
* - Liste des codes html des occurrences de la balise, sinon tableau vide * - Liste des codes html des occurrences de la balise, sinon tableau vide
* - Tableau de résultats, si tableau en entrée. * - Tableau de résultats, si tableau en entrée.
**/ **/
function extraire_balises($texte, $tag = 'a') { function extraire_balises($texte, $tag = 'a', $options = []) {
if (is_array($texte)) { if (is_array($texte)) {
array_walk( array_walk(
$texte, $texte,
...@@ -2496,7 +2498,7 @@ function extraire_balises($texte, $tag = 'a') { ...@@ -2496,7 +2498,7 @@ function extraire_balises($texte, $tag = 'a') {
} }
$htmlTagCollecteur = new \Spip\Texte\Collecteur\HtmlTag($tag); $htmlTagCollecteur = new \Spip\Texte\Collecteur\HtmlTag($tag);
$collection = $htmlTagCollecteur->collecter($texte); $collection = $htmlTagCollecteur->collecter($texte, $options);
if (!empty($collection)) { if (!empty($collection)) {
return array_column($collection, 'raw'); return array_column($collection, 'raw');
} }
......
...@@ -49,6 +49,7 @@ class HtmlTag extends AbstractCollecteur { ...@@ -49,6 +49,7 @@ class HtmlTag extends AbstractCollecteur {
* @param string $texte * @param string $texte
* @param array $options * @param array $options
* bool $detecter_presence * bool $detecter_presence
* bool $nb_max
* @return array * @return array
*/ */
public function collecter(string $texte, array $options = []): array { public function collecter(string $texte, array $options = []): array {
...@@ -130,7 +131,10 @@ class HtmlTag extends AbstractCollecteur { ...@@ -130,7 +131,10 @@ class HtmlTag extends AbstractCollecteur {
$tags[] = $tag; $tags[] = $tag;
} }
} }
if (!empty($options['detecter_presence']) and count($tags)) { if (
(!empty($options['detecter_presence']) and count($tags))
or (!empty($options['nb_max']) and count($tags) >= $options['nb_max'])
) {
return $tags; return $tags;
} }
} }
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter