Regular expressions
Hallo,
ich möchte aus einem string z.B.:
"
text text text text text
text text text text text
hallo-das hier-hallo
text text text text text
text text text text text
"
das "das hier" "herrausfiltern". es soll anschließend als ziel eines links erscheinen.
ich habe zunächst mit
preg_replace("!hallo-(*.?)-hallo!","<a href=\"$1\">link</a>", $string);
den link erstellt... allerdings möchte ich, dass ebenfalls der gesammte andere text auch "verschwindet"..
hat jemand eine Idee? (und entschuldigung für das chaos)
MfG
Thomas
Antwort schreiben
Antwort 1 von Sichtbarer vom 28.04.2019, 20:08 Options
$str = "text text text text text
text text text text text
hallo-das hier-hallo
text text text text text
text text text text text";
$strs ="hallo"; //das Wort, das dein Zielbegriff umrandet (ohne "-" !)
$start = strpos($str,$strs) + strlen($strs) + 1;
$end = strrpos($str,$strs)-1;
$length = $end - $start;
$give = substr($str,$start,$length);
echo $give;
Antwort 2 von thomas_w vom 28.04.2019, 23:13 Options
Theoretisch sollte es so funktionieren, aber ich habe es selbst
schon auf diese Art probiert. Leider vergebens. Ich habe das
Problem jetzt mit preg_match_all() gelöst.
Falls Interesse besteht kann ich den Code gerne posten.
Trotzdem danke!
Antwort 3 von Sichtbarer vom 28.04.2019, 23:19 Options
Wäre klasse wenn du den kurz posten könntest. Würde mich schon interessieren.
Sichtbarer.
Antwort 4 von thomas_w vom 29.04.2019, 08:57 Options
$str=preg_match_all('#hallo-(.+)-hallo#Uis',$cont,$res);
foreach($res[1] as $res){
echo "<a href=\"".$res."\">link</a>";};
wobei hier der gegebene String "$cont" heißt....