Name : BETA-TESTERS
Project name : ZW3B-API-BETA-TESTERS
Authorized. - 200 - Client API Name and Origin Wildcard OK
Fonction PHP pour traduire les liens Web, les facebook page et les hastag - expressions régulieres
Bonjour,
J'ai écris 2,3 fonctions PHP pour récupérer et pouvoir linker les pages FaceBook de mes publications.
Je récupére les archives de mon compte personnel FaceBook O-Romain Jaillet-ramey (Kss Operating Society Solar) sur cette page : Paramètres > télécharger vos informations personnelles sur cette page en JSON.
Ici, je parse les liens web ou les adresses emails.
Script avec 12 lignes
001<?php 002function link2href($desc) { 003 004 $desc = preg_replace("#[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]#",'<a href="${0}">${0}</a>', $desc); 005 $desc = preg_replace("#(^| )(www([.]?[a-zA-Z0-9_/-])*)#", '${1}<a href="http://${2}">${2}</a>', $desc); 006 $desc = preg_replace("#[A-Za-z0-9.|-|_]*[@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}#", replace_mail('${0}'), $desc); 007 //$desc = str_replace("&", "&", $desc); 008 //$desc = utf8_encode($desc); 009 010 return $desc; 011 } 012?>
Ici, je parse les hastag ou les pages/users ID facebook.
Script avec 10 lignes
001<?php 002function link2facebookpage_hastag($desc) { 003 004 $desc = preg_replace('/(^|[ \n\r\t])\#([^0-9]{1}[A-Za-z0-9_ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ]+)#/is', ' <a href="https://www.facebook.com/hashtag/${1}" title="FB Hastag : ${1}" target="_blank">#${1}</a> ', $desc); 005 $desc = preg_replace('/(?<!=|\b|&)\@\[([a-z0-9]+)+([:]{1})+([0-9]{2,4})+([:]{1})+([^]]+)\]/is', '<a href="https://www.facebook.com/${1}" title="FB Page : ${5}" target="_blank">${5}</a>', $desc); 006 //$desc = utf8_encode($desc); 007 008 return $desc; 009} 010?>
Exemple de pages ID FaceBook :
Script avec 224 lignes
001@[1602794995:2048:O-Romain Jaillet-ramey] 002@[1514534735466599:274:LAB3W] 003@[723926421024294:274:ZW3B] 004@[100063551741259:2048:LinuxFr.org] 005@[260694400660026:274:Santé+ Magazine] 006@[110871935746359:274:60 millions de consommateurs] 007@[388223307574:274:Nice-Matin] 008@[162422610475091:274:Monaco-Matin] 009@[123440511000645:274:Les Echos] 010@[49173930702:274:Le Point] 011@[51555073310:274:20 Minutes] 012@[110766378956229:274:20 minutes online] 013@[387606894638793:274:News] 014@[41939437980:274:7sur7.be] 015@[100063699725868:2048:Qarnot computing] 016@[1535230416709539:274:Macron] 017@[100044157757824:2048:Emmanuel Macron] 018@[100044604601832:2048:Nicolas Sarkozy] 019@[100063510793005:2048:Vatican] 020@[100064304493546:2048:Bon Clic Bon Genre] 021@[102103186496772:274:Ville de Cannes] 022@[41939437980:274:7sur7.be] 023@[100064312826085:2048:3DVF] 024@[100064489492306:2048:Unity] 025@[100063572834049:2048:InkedGeek] 026@[573062086053987:274:Walt Disney Studios] 027@[197710070249937:274:Festival de Cannes] 028@[100044366322167:2048:Marion Cotillard FR] 029@[100044181681909:2048:Jennifer Garner] 030@[100044561913849:2048:Interpol] 031@[100044382939074:2048:Cameron] 032@[100044490817001:2048:Lucy] 033@[100044506233470:2048:Drew] 034@[741659586:2048:Olivia] 035@[100044312202140:2048:Sosenka] 036@[100044203430980:2048:Leonardo DiCaprio] 037@[100044427996589:2048:Guillaume Canet] 038@[100044620500821:2048:Laetitia Casta] 039@[100044321951905:2048:Stephen Marley] 040@[100044506623316:2048:Julian Marley] 041@[100044381351120:2048:Damian Marley] 042@[11608793743:274:Madame Figaro] 043@[39130635629:274:Femme Actuelle] 044@[100064304493546:2048:Bon Clic Bon Genre] 045@[162588269536:274:Gala] 046@[162135926640:274:Voici] 047@[100047775885066:2048:Reworld Media] 048@[100069103302085:2048:Media365] 049@[106300863471:274:Sport365] 050@[191478637535798:274:Femina] 051@[301300799953924:274:beIN SPORTS France] 052@[569673610:2048:Romain Schindler] 053@[720442934:2048:Vanessa Le Moigne] 054@[109218389152020:274:PopCrush] 055@[103480985513087:274:MetaWeek] 056@[48991161340:274:Linus Torvalds] 057@[693844217309942:274:Internet Live Stats] 058@[763390373829810:274:World Wide Web] 059@[100064028619684:2048:WebRankInfo] 060@[270122530294:274:Ouest France] 061@[169005736520113:274:CERN] 062@[100069532598100:2048:CNIL - Commission Nationale de l'Informatique] 063@[20286838141:274:Netcraft] 064@[204782419591139:274:AFTI - Groupe Aforp] 065@[109003629123261:274:CNRS] 066@[100064523628729:2048:Imperial College London] 067@[1378350049065059:274:Freecodecamp] 068@[30999986172:274:JV] 069@[262134952380:274:Mozilla] 070@[142368195909703:274:Les Enfants de MacGyver / Children of MacGyver] 071@[100044444949139:2048:Korben] 072@[681156471908541:274:Delicious Brains] 073@[1680111352220478:69:Symfony Framework PHP community] 074@[128562817166981:274:JetBrains] 075@[234206136670153:274:Zdnet.com] 076@[70693797649:274:01net] 077@[100063610146942:2048:GNU/Linux Magazine] 078@[103584641194256:274:9to5Linux] 079@[9801962186:274:Debian] 080@[65878196434:274:How-To Geek] 081@[115098615297581:274:It's FOSS] 082@[106159169414409:274:Cité du Vatican] 083@[96758232749:274:Clubic] 084@[59230744796:274:Winamp] 085@[278029242253904:274:Twitter] 086@[155679114453319:274:Numerama] 087@[910308105687763:274:Lapresse.ca] 088@[100063676026107:2048:Cath.ch] 089@[101102083280258:274:Phonandroid] 090@[100069966050501:2048:Developpez] 091@[177159822466824:274:Programmez] 092@[124436167285:274:CSS-Tricks] 093@[100063234847857:2048:Univers Freebox] 094@[652509114777647:274:Hitek] 095@[103228751739360:274:Linux Shell Tips] 096@[273223472712721:274:IT-Connect] 097@[100069679221333:2048:It Connect] 098@[100063341581283:2048:IT-Connect] 099@[149515305173840:274:DigitalOcean] 100@[120089902710653:274:CloudSavvy] 101@[207351519295239:274:TrueNAS Community] 102@[109729960455135:274:Proxmox] 103@[13804404915:274:Radionomy] 104@[232990460221636:274:ProtonMail] 105@[121623401752:274:BleepingComputer] 106@[255370230787:274:Le Journal du Geek] 107@[204045139612793:274:Journal du Net] 108@[100082343340023:2048:Leo TechMaker] 109@[228055448622089:69:Physique Quantique , ufologie , et Etres de lumière] 110@[101401456710000:274:Raspberry Pi] 111@[83217870837:274:Futura - Explorer le monde] 112@[691548691002147:274:Trust My Science par J. Paiano] 113@[194705390569710:274:Sciences et Avenir] 114@[103730576353166:274:Daily Geek Show] 115@[100064361936789:2048:Phys.org] 116@[104677048718:274:Xerfi] 117@[134777716591387:274:Techno-Science.net] 118@[100044361762648:2048:Brian Greene] 119@[1173492829458791:274:Les Cahiers de Science & Vie] 120@[100066961399786:2048:NASA's] 121@[57242657138:274:NASA Earth] 122@[100064958731093:2048:NASA Mars] 123@[100064770034135:2048:NASA Technology] 124@[100044561550831:2048:NASA - National Aeronautics and Space Administration] 125@[168170833215856:274:International Space Station] 126@[364362536909391:274:International Space University ISU] 127@[110165025679345:274:Agence spatiale europeenne] 128@[54912575666:274:ESA - European Space Agency] 129@[289942951345417:274:ISS83] 130@[116744118373373:274:Research Institute of Computer Science and Random Systems] 131@[17043549797:274:MIT Technology Review] 132@[235877164588:274:New Scientist] 133@[415712035190005:69:Elon Musk] 134@[100049914979213:2048:Elon Musk] 135@[129335016182:274:Avatar] 136@[34610502574:274:TF1] 137@[121764658163520:274:TF1 Le JT] 138@[237212083471334:274:MYTF1] 139@[179086202130933:274:France Télévisions] 140@[135112586936434:274:Franceinfo] 141@[107116979321299:274:Capital] 142@[153632746935:274:FRANCE 24] 143@[43896752783:274:BFMTV] 144@[76952916976:274:CNEWS] 145@[155869377766434:274:NBC News] 146@[93889432933:274:CNA] 147@[14892757589:274:Le Monde] 148@[130322493652366:274:Fredzone] 149@[100068323101812:2048:Le monde informatique] 150@[1352794918124335:274:Solubag] 151@[89047151333:274:Presse-citron] 152@[100064763198425:2048:Network World] 153@[1594820090798208:274:Google] 154@[312923265400622:274:Google Paris] 155@[189463268145947:274:Google AdSense] 156@[955037507942229:274:Google Ads] 157@[102258288460:274:Google Analytics] 158@[648085195238332:274:Google Quantum AI Lab] 159@[967415219957038:274:Google Developers] 160@[633746319978236:274:Siècle Digital] 161@[100152585861191:274:Viture] 162@[44221826665:274:Les Numériques] 163@[245445838963467:274:LesNumeriques] 164@[100024822155635:2048:Laetitia Klopocki] 165@[100044524445711:2048:Shenseea] 166@[137524096263934:274:Abondance.com] 167@[190010265716:274:Cisco Networking Academy] 168@[317333380357:274:Arobasenet] 169@[59061450398:274:WebRankInfo] 170@[110004031372:274:Webmarketing & co'm] 171@[31928183984:274:Frandroid] 172@[100845797055125:274:LinkedIn] 173@[124136227972:274:BDM] 174@[100063979604144:2048:Natashawax] 175@[100044383237893:2048:Paul Kalkbrenner] 176@[304875692979410:274:L'Usine Digitale] 177@[100064301424717:2048:CNIL - Commission Nationale de l'Informatique et des Libertés] 178@[172819872731894:274:The Hacker News] 179@[100064300644661:2048:Ministère de l'Intérieur] 180@[103146727990:274:Ministère de l'Intérieur] 181@[196708717010864:274:Ministère de la Justice] 182@[100064312374236:2048:Ministère de la Justice] ? 183@[127027683999474:274:Ministère des Armées] 184@[100064492067838:2048:Armée de Terre] 185@[100064809783613:2048:Marine nationale] 186@[100064571114642:2048:Armée de l'Air et de l'Espace] 187@[110774245616525:274:Paris] 188@[761337517297862:274:Paris 2024] 189@[176770602367245:274:Armée de l'Air et de l'Espace] 190@[164895519685:274:EASA - European Union Aviation Safety Agency] 191@[100064303167234:2048:Ministère de l'Education nationale, de la Jeunesse et des Sports] 192@[119095054839458:274:Ministère de l'Education nationale, de la Jeunesse et des Sports] 193@[133782623464003:274:IESF - Ingénieurs et Scientifiques de France] 194@[126533127390327:274:Massachusetts Institute of Technology (MIT)] 195@[259978937401589:274:Police Nationale] 196@[207735435911954:274:Help Net Security] 197@[323145771376309:274:VulDB] 198@[312308895775296:274:Analytics Insight] 199@[118123311617759:274:Les-Crises] 200@[178053755684738:274:Agence nationale de la sécurité des systèmes d'information] 201@[199446050118539:274:PP.Bizness] 202@[304131540069061:274:Mixed Reality TV] 203@[471222269878506:274:TFlex Tech] 204@[102437378960635:274:Le Journal du Coin - Actu Bitcoin, Cryptomonnaie & Blockchain] 205@[128434437780493:274:Cryptoast - Bitcoin, Crypto et Blockchain] 206@[103373896380515:274:Le Monde Informatique] 207@[61261101338:274:Le Figaro] 208@[110503495636131:274:Lissac, l'opticien qui change la vue] 209@[181905338671811:274:Baidu Inc.] 210@[101729051831529:274:Maximus Supply & Technology] 211@[227779547277549:274:BeGeek] 212@[100858346620905:274:Udemy] 213@[169754586411698:274:Aéroport Nice Côte d'Azur] 214@[118474648763784:274:Vallee De La Tinée] 215@[238580719326:274:Valdeblore] 216@[110667328960124:274:Saint-Dalmas-Valdeblore, Provence-Alpes-Cote D'Azur, France] 217@[100035913002052:2048:Commune Saint Martin Vésubie] 218@[843207632437421:274:Vallée de la Roya: destins oubliés] 219@[579182425497344:274:Netflix] 220@[198918683904639:274:Les voyageurs du temps] 221@[299972228886:274:GQ France] 222@[1006411482778121:274:FedEx] 223@[100089764414429:2048:Fly Music] 224@[100064489492306:2048:Unity]
Dans cet exemple on transforme le l'arobase des adresses mails.
Script avec 16 lignes
001<?php 002function replace_mail($email) { 003 004 $codes[0000] = '/@/'; 005 $codes[0001] = '/./'; 006 $replace[0000] = ' (at) '; 007 $replace[0001] = ' (dot) '; 008 009 $transform = preg_replace($codes, $replace, $email); 010 011 // on creait le lien ou non 012 //$transform = '<a href="mailto:'.mailto($email).'">'.$transform.'</a>'; 013 014 return $transform; 015} 016?>
Encodage de l'email :
Script avec 12 lignes
001<?php 002function mailto($email) { 003 004 $encoded = bin2hex("".$email.""); 005 $encoded = chunk_split($encoded, 2, '%'); 006 $encoded = '%' . substr($encoded, 0, strlen($encoded) - 1); 007 008 $mail = $encoded; 009 010 return $mail; 011} 012?>
Exemple d'appel :
Script avec 10 lignes
001<?php 002$text_default = 'Ci-dessous des fonctions#PHP pour parser les liens Web https://www.zw3b.fr et les adresses email@domain.tld ;) \n\nCordialement,\nRomain (@[1514534735466599:274:LAB3W]) - Creator @[723926421024294:274:ZW3B].FR .TV .SITE .NET .BLOG\n\n'; 003 004$text = link2facebookpage_hastag(link2href($text_default)); 005 006$text = nl2br($text); 007 008 009echo $text; 010?>
Si cela peut servir à quelqu'un.
Romain
preg_replace
- Rechercher et remplacer par expression rationnelle standard
nl2br
- Insère un retour à la ligne HTML à chaque nouvelle ligne