PHP-Einfach.de
  • PHP Tutorial
  • MySQL Tutorial
  • Für Fortgeschrittene
  • Webhosting
  • Forum

URLs im Text identifizieren

10. Februar 2020
  1. Home
  2. »
  3. Für Fortgeschrittene
  4. »
  5. Script-Beispiele
  6. »
  7. URLs im Text identifizieren

Ein häufiges Features welches man auf fast jeder Website findet ist die automatische Identifizierung von URLs und E-Mail-Adressen im Text und die automatische Umwandlung dieser in entsprechend anklickbare Hyperlinks. Dieser Artikel zeigt euch, wie dies mittels regulären Ausdrücken funktioniert.

URLs identifizieren

Wir verwenden reguläre Ausdrücke um potentielle URLs zu identifizieren.  Für jede gefundene URL fügen wir am Anfang ein [URL] hinzu und am Ende ein [/URL] hinzu. Anschließen lassen wir alle diese URL's ersetzen. Dies hat den Vorteil, dass der Besucher selbst bestimmen kann, wie der Link Text aussieht, indem er z.B. schreibt: [URL=http://www.php-einfach.de]PHP lernen leicht gemacht[/URL].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$text = "http://www.php-einfach.de";
 
$urlsuch[]="/([^]_a-z0-9-=\"'\/])((https?|ftp):\/\/|www\.)([^ \r\n\(\)\^\$!`\"'\|\[\]\{\}<>]*)/si";
$urlsuch[]="/^((https?|ftp):\/\/|www\.)([^ \r\n\(\)\^\$!`\"'\|\[\]\{\}<>]*)/si";
 
$urlreplace[]="\\1[URL]\\2\\4[/URL]";
$urlreplace[]="[URL]\\1\\3[/URL]";
 
$text = preg_replace($urlsuch, $urlreplace, $text);
 
 
// Geben wir den Text jetzt aus,
// so erhalten wir als Ausgabe: [URL]http://www.php-einfach.de[/URL].
 
// Jetzt kommt der nächste Schritt, die URL's werden zu Links:
 
$text = preg_replace("/\[URL\]www.(.*?)\[\/URL\]/si", "<a target=\"_blank\" href=\"http://www.\\1\">www.\\1</a>", $text);
 
$text = preg_replace("/\[URL\](.*?)\[\/URL\]/si", "<a target=\"_blank\" href=\"\\1\">\\1</a>", $text);
 
echo $text;
?>

E-Mail-Adressen identifizieren

Ähnliches können wir mit E-Mail-Adressen anstellen. Finden wir eine potentielle E-Mail-Adresse im Text, beispielsweise [email protected], so wandeln wir diese in einen Hyperlink direkt um. Der entsprechende reguläre Ausdruck sieht dafür wie folgt aus:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$text = "[email protected]";
 
$emailsuch[]="/([\s])([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
 
$emailsuch[]="/^([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,}))/si";
$emailreplace[]="\\1[EMAIL]\\2[/EMAIL]";
$emailreplace[]="[EMAIL]\\0[/EMAIL]";
 
if (strpos($text, "@")){
    $text = preg_replace($emailsuch, $emailreplace, $text);
}
 
//E-Mail Adressen werden zu links
$text = preg_replace("/\[EMAIL\](.*?)\[\/EMAIL\]/si", "<a href=\"mailto:\\1\">\\1</a>", $text);
 
$text = preg_replace("/\[EMAIL=(.*?)\](.*?)\[\/EMAIL\]/si", "<a href=\"mailto:\\1\">\\2</a>", $text);
 
echo $text;
?>

 

Autor: Nils Reimers
Zurück: Logfiles mit PHP generieren
Weiter: Codeschnipsel

Für Fortgeschrittene

  • Objektorientierte Programmierung
  • PHP Sicherheit
  • Script-Beispiele
    • Loginscript
    • E-Mails mit Anhang versenden
    • Gästebuch
    • PDFs erstellen
    • Besucherzähler mit Reload-Sperre
    • Kontaktformular
    • Logfiles mit PHP generieren
    • URLs im Text identifizieren
  • Codeschnipsel
  • Stellenmarkt
Mit freundlicher Unterstützung von:
  • Punkt191 Werbeagentur
  • DGUV V3
  • DGUV 3
  • CasinoAndy Finland
  • Casinopilot24.com
  • Neueonline-Casinos.com
  • CasinoHEX.at
  • Decasinos.de
  • Privatkredit247.com
  • Online Casino Spielautomaten
  • Casinofrog.com
  • parhaatuudetkasinot.com

Hoster – Geringste Ausfallzeit

  1. webgo Ø 0 Min.
  2. Linevast Ø 3 Min.
  3. netcup Ø 3 Min.
  4. dogado Ø 6 Min.
  5. Mittwald Ø 9 Min.
  6. All-Inkl.com Ø 10 Min.
  7. manitu Ø 10 Min.
  8. bplaced Ø 11 Min.
  9. checkdomain Ø 11 Min.
  10. Host Europe Ø 14 Min.
» Mehr erfahren

Impressum | Datenschutz | Auf PHP-Einfach.de werben

© PHP-Einfach.de 2003 - 2022

Um dich beim Lernen von PHP und MySQL zu unterstützen verwenden wir Cookies. OK Weitere Infos
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Notwendige
immer aktiv

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Nicht notwendige

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.