Yêu cầu thg 11 7, 2020 5:28 SA 253 0 2
  • 253 0 2
0

Cách kiểm tra chuỗi có từ bắt đầu bằng dấu @ trong PHP

Chia sẻ
  • 253 0 2

Mình muốn kiểm tra chuỗi để xử lý bất kỳ từ nào bắt đầu bằng dấu @.

$string = "Hello, I am @Tom . I'm a cat. This is @Jerry .";  //trường hợp 1
$string = "Hello, this is <a href='https://website.com/user/Tom'>@Tom</a>"; //trường hợp 2
$string = "Hello, my email is tomcat@jerry.com"; //trường hợp 3

Làm sao để mình chỉ thu được kết quả như ở trường hợp 1, còn trường hợp 2 và 3 thì bỏ qua. preg_match('@', $string) dường như không ổn.

(Mục đích cuối cùng là khi người dùng gõ văn bản có dấu @Nickname thì sẽ chuyển thành đường link dẫn tới tài khoản đã đăng kí.)

Oh! Chữ Nickname ở trên đã chuyển sang dạng link, y như cái mong đợi.

Test email: abcd@badsd.com

Avatar Dr Dokkan @dokkan7
thg 11 7, 2020 6:40 SA

Mình dùng

$text = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://website.com/user/$1\">@$1</a>", $string);
echo $text;

Nhưng không loại được trường hợp dấu @ nằm ở giữa từ ví dụ như trong email.

2 CÂU TRẢ LỜI


Đã trả lời thg 11 9, 2020 9:38 SA
Đã được chấp nhận
+4

Theo mình hiểu thì bạn muốn match @nickname chỉ khi trước @ không phải là một ký tự nào đó (mà phải là whitespace) đúng không nhỉ 😄

Bạn thử cách này xem sao nhé, mình cũng chỉ sửa lại một chút từ cách làm trong comment của bạn thôi 😃

function replaceMention($string) {
    // Replace nếu string bắt đầu bằng @   
    $string = preg_replace("/^@([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://website.com/user/$1\">@$1</a>", $string);
    // Replace trong các trường hợp @ nằm bên trong văn bản, và phía trước nó phải là ký tự thuộc kiểu whitespace
    $string = preg_replace("/(\s)@([A-Za-z0-9\/\.]*)/", "$1<a target=\"_new\" href=\"http://website.com/user/$2\">@$2</a>", $string);    
    return $string;
}

$string1 = "Hello, I am @Tom. I'm a cat. This is @Jerry.";
$string2 = "Hello, this is <a href='https://website.com/user/Tom>@Tom</a>";
$string3 = "Hello, my email is tomcat@jerry.com";
$string4 = "@Tom: hello, are you @Jerry?";
echo replaceMention($string1) . "\n";
echo replaceMention($string2) . "\n";
echo replaceMention($string3) . "\n";
echo replaceMention($string4) . "\n";

Output

Hello, I am <a target="_new" href="http://website.com/user/Tom.">@Tom.</a> I'm a cat. This is <a target="_new" href="http://website.com/user/Jerry.">@Jerry.</a>
Hello, this is <a href='https://website.com/user/Tom>@Tom</a>
Hello, my email is tomcat@jerry.com
<a target="_new" href="http://website.com/user/Tom">@Tom</a>: hello, are you <a target="_new" href="http://website.com/user/Jerry">@Jerry</a>?

Bạn thử lại xem có ổn không 🤔

Chia sẻ
Avatar Dr Dokkan @dokkan7
thg 11 11, 2020 7:50 SA

Chào bạn. Mình đã test và thấy code của bạn chạy ổn.

Lưu ý: bạn nào sau này thấy bài viết này thì cần chú ý là username không nên dính các ký tự đặc biệt như ',",!"/, tốt nhất là ở dạng alphanumeric.

Avatar com streamvn @streamvn.pay
thg 11 24, 2020 2:36 CH

Đúng cái đang cần, cảm ơn bác Teen Porn Tube, Young Sex, Porn Videos, Free Young Porn Videos, Young Porn Videos, XXX Teen Porn, Young Sex Movies, Teen Clips, Teen Porno https://www.kuxxx.com/categories.html https://www.kuxxx.com/videos/top https://www.kuxxx.com/videos/trending https://www.kuxxx.com/videos/category/662 https://www.kuxxx.com/watch/xvideos-full-hd-xnxx-porn-xxx-free-xhamster-full-porno-videos-xxx-tube-porn-videos_Iijoy9UKKKxc7YZ.html https://www.kuxxx.com/watch/xvideos-xnxx-xhamster-full-porno-videos-xxx-tube-porn-videos_Iijoy9UHHHxc7YZ.html https://clipmp4.com/categories.html https://clipmp4.com/videos/top https://clipmp4.com/videos/trending https://clipmp4.com/watch/xvideos-xnxx-pornhub-free-hd-xhamster-full-porno-videos_BuSXnBDaTO53PR5.html https://clipmp4.com/watch/xvideos-com-xnxx-com-pornhub-com-xhamster-com-full-porno-videos_dKpYkJGFUkhxhQf.html https://xxxuh.com/categories.html https://xxxuh.com/videos/top https://xxxuh.com/watch/xvideos-free-hd-xnxx-porn-free-xhamster-pornhub-full-porno-videos_Iijoy9UHHHxc7YZ.html https://xxxhot.net/ https://xxxhot.net/videos/amateur phim sex hay, phim sex loạn luân, phim sex việt nam, phim sex hay, phim sex thuyết minh https://www.uhxxx.com/xvideos-com-nhung-bo-phim-sex-em-yeu-co-body-mong-to-cuc-hap-dan/ https://www.uhxxx.com/tong-hop-phim-sex-xnxxcom-xvideos-va-thiendia-hay-nhat/ https://www.uhxxx.com/phim-sex-tieng-viet-thuyet-minh-moi-nhat-2020/ https://www.videovz.com/ https://www.videovz.com/category/phim-sex-chau-a/ https://www.videovz.com/category/phim-sex-viet-nam/ https://www.videovz.com/phim-sex-xvideos-com-xnxx-com-moi-nhat-chon-loc-hang-ngay/

https://forum.opencart.com/viewtopic.php?f=169&t=136137&sid=13a34e79a5a5c344e393d4ba4e135f93&start=20 http://danketoan.com/threads/tang-file-tinh-he-so-nguy-co-pha-san-altman-z-score-tu-dong.279463/page-3 http://kythuatphancung.vn/forum/index.php?/profile/121796-17phuclai/&tab=field_core_pfield_11 http://kythuatphancung.vn/forum/index.php?/topic/41333-xin-driver-cand-trung-quốc/ https://ftu-forum.org/diendan/threads/nhts2019-trung-tam-phat-trien-quoc-te.8713/ https://vietfones.vn/forum/threads/ipad-3-dinh-icloud.1691524/ https://www.otofun.net/threads/xe-vinfast-dang-giam-ve-muc-ba-khong.1658622/page-5#post-54458023 http://webdien.com/d/showthread.php?t=342&page=5&p=435928#post435928 https://www.techrum.vn/threads/tổng-hợp-các-địa-chỉ-tải-ios-firmware-uy-tín-nhất-hiện-nay-dành-cho-idevices.149774/#post-923214 http://forum.gamevn.com/threads/dao-nay-box-cha-co-them-game-gi-moi-nhi.1346278/page-288#post-29606407 https://www.freehostforum.com/forum/advertising-forums/webmaster-marketplace/paid-web-hosting-offers-and-requests/dedicated-hosting-offers-and-requests/608577-quad-core-xeon-e3-e5-ssd-dedicated-servers-deals?p=608578#post608578 https://www.smallbusinessforums.org/showthread.php?15862-Hello-everyone&p=127478#post127478 https://bitcointalk.org/index.php?topic=676942.msg53903465#msg53903465 http://www.dientuvietnam.net/forums/forum/Điện-tử-viễn-thông-và-hệ-thống-mạng/tổng-quan-về-ngành-viễn-thông/178317-công-suất-dòng-điện-dc/page2#post1715853 https://forum.xnxx.com/threads/a-to-z-songs.205613/page-2235#post-12019473 https://www.linkedin.com/pulse/xxx-porn-da-maz/?published=t https://streamvnad.tumblr.com/ https://www.reddit.com/user/General_Time3841/comments/jdhprv/xxx_videos_porn/ https://kuxxx.weebly.com/ https://www.blackforum.org/threads/get-adult-video-script-avs-v8-1.16/page-4 https://www.avsdemo.com/blog/1007 https://viblo.asia/q/cach-lay-tu-cum-tu-dau-tien-trong-mot-chuoi-phan-cach-bang-dau-phay-trong-php-P0lPPaoblox https://viblo.asia/p/security-testing-tutorial-part-3-encoding-decoding-and-cryptography-m68Z07QNKkG https://pornhdxxx.joomla.com/ http://www.folkd.com/user/streamvn.ad https://www.bizvotes.com/ca/oakland/cleaning-janitorial-services/watch-online-xvideos-hd-full-xnxx-beeg-pornhub-freya-porn-adult-traffic-1847241.html https://torgi.gov.ru/forum/posts/list/0/131968.page#181698 https://www.joomlacommunity.nl/forum/overige-vragen/uitklapmenu-categorieen-vastzetten https://www.ultimate-guitar.com/forum/showthread.php?t=2011181#13 https://community.shopify.com/c/Technical-Q-A/Newsletter-and-Contact-form-redirecting-to-quot-404-Not-Found/m-p/857632#M32830 https://answers.yahoo.com/question/index?qid=20201101030238AA0LRI6 https://www.freeadzforum.com/index.php?topic=107077.msg127501#msg127501 https://www.skyscrapercity.com/threads/photos-youve-taken.439957/page-296#post-170462226 https://blenderartists.org/t/future-of-instancing-and-multithreading-question/1262774/9 https://www.shroomery.org/forums/showflat.php/Number/27016712/vc/1#27016712 https://forums.gamershood.com/forum/free-online-offline-games/role-playing-games/11417-?p=1962929#post1962929 https://cboard.cprogramming.com/c-programming/179686-how-clear-screen-after-short-pause.html http://www.thefreeadforum.com/postclassifieds/user/profile/382918 http://www.thefreeadforum.com/postclassifieds/for-sale/video-games-consoles/porno-videos_i2539757 https://forum.shoppingtelly.com/threads/flexi-pay.61140/post-992699 https://sailingforums.com/threads/before-and-after-pics.42982/ https://www.theverge.com/users/SuBay-porn https://profiles.delphiforums.com/n/pfx/profile.aspx?webtag=dfpprofile000&userId=1891095061 http://www.debate.org/streamvn/ http://forums.debian.net/viewtopic.php?f=20&t=144202&p=729907#p729907 https://forum.arduino.cc/index.php?topic=699968.msg4784559#msg4784559 https://forum.undernet.org/viewtopic.php?f=4&t=23106 https://www.techrepublic.com/forums/discussions/question-about-purchasing-a-pc/ https://www.mac-forums.com/threads/the-ios14-app-library-love-it-or-hate-it.360651/#post-1868719 http://www.geekstogo.com/forum/topic/375364-win-10-update-14102020-causing-black-screen/?p=2654218 https://www.digitalstorm.com/forums/what-are-you-listening-to-right-now-tidf30040-pidf380538/#380538 https://www.digitalstorm.com/forums/video-editing-tidf31377-pidf380539/#380539 https://forum.lowyat.net/index.php?showtopic=2512211&st=220&p=98870418&#entry98870418 https://www.allkpop.com/forum/threads/irene-seulgi-monster-ep-album-review.454466/page-2#post-9063498 https://www.kanyetothe.com/threads/your-best-most-nostalgic-gaming-memories.8260307/post-542303378 https://www.marketingprofs.com/ea/qst_question.asp?qstid=48095&posted=1#283486 https://cryptocurrencytalk.com/topic/165926-how-to-start-mining/?tab=comments#comment-583695 https://forums.macrumors.com/threads/stuck-with-windows-10.2264699/?post=29166791#post-29166791 https://www.pricescope.com/community/threads/how-many-piercings-do-you-have.251158/page-2#post-4848899 https://forum.topeleven.com/tutorials-guides/80830-why-legal-create-mutant-player.html#post673745 https://www.craftsforum.co.uk/forum/your-crafts/general/1119113-with-coronavirus-fully-in-the-uk-now-will-crafts-see-a-growth?p=1123563#post1123563 https://independent.academia.edu/KMr13 http://www.authorstream.com/streamvn.ad/ https://peatix.com/user/6956592/view https://speakerdeck.com/streamvn https://www.storeboard.com/baysu http://www.wysp.ws/streamvn/ https://gotartwork.com/Profile/anh-nguyen/68817/ https://www.wishlistr.com/profile/streamvn http://forum.cloudme.com/profile.php?id=153802 https://topsitenet.com/profile/streamvn/87710/ https://www.magcloud.com/user/streamvn https://forum.gsmhosting.com/vbb/f1086/im-looking-iphone-11-series-tbk-laser-machine-ezd-file-2836191/#post14258837 https://www.affiliatefix.com/threads/youtube-mastering-and-monetization-for-your-site-hustle.165368/#post-634188 https://www1.flightrising.com/forums/gend/2926041/4#post_45244632 https://www.otosaigon.com/threads/dap-xe-du-lich-ho-ta-nung-dak-nong-mien-dat-yen-binh-cua-nguoi-mnong.8993325/#post-18657183 https://forum.mikrotik.com/viewtopic.php?f=21&t=168411 https://nulledbb.com/thread-PopCorn-WP-Adult-theme?pid=1373422#pid1373422 https://forums.androidcentral.com/showthread.php?t=941379&p=6885382&viewfull=1#post6885382 https://mygicasupport.com/index.php?/topic/4856-t230c-can-not-work-when-resume-from-hibernationsleep-try-this-driver/#replyForm https://forums.mtbr.com/beginners-corner/am-i-too-old-wheelie-727102.html#post15080647 https://forums.mtbr.com/beginners-corner/first-time-bikepark-what-protection-get-1100120.html#post15080651 https://www.vbforums.com/showthread.php?882867-Corona-virus-China-major-city-lockdowns-etc&p=5499566#post5499566 https://bethesda.net/community/topic/389369/the-evil-within-key-bindings/2?language%5B%5D=en&language[]=en https://www.informatik-forum.at/forum/index.php?thread/116765-auslandssemester/&postID=851152#post851162 https://www.spielereihoch3.de/index.php?thread/214-rezensionen-im-blog-vom-spielecafé-der-generationen/#post2593 https://www.adventurespiele.net/index.php?module=pnForum&func=viewtopic&topic=3646&start=0#pid32374 https://www.tripadvisor.com/ShowTopic-g8766828-i36265-k10686130-Lake_Champlain_Kayaking_Companion_Wanted-Lake_Champlain.html#108511180 https://www.mysteryshopforum.com/read/2/815137/815298#msg-815298 https://www.betriebsrat.de/forum/thread/30952-was-hört-ihr-gerade/?pageNo=2#post204234 https://myanimelist.net/forum/?topicid=1878889&show=100#msg61287536 https://myanimelist.net/forum/?topicid=1879080&show=50#post99 https://py.checkio.org/forum/post/12531/assertion-error/#comment-94462 https://www.steinberg.net/forums/memberlist.php?mode=viewprofile&u=296532 https://www.mobafire.com/league-of-legends/forum/videos-and-streams/one-league-trick-which-will-increase-your-chances-to-win-a-laning-phase-44186#lastcomment https://software.informer.com/forum/post/38624/#p38624

https://ello.co/streamvn https://coolors.co/u/anh_nguyen10 https://500px.com/p/streamvnad?view=photos https://codepen.io/streamvn https://about.me/anhmr https://www.spreaker.com/user/pornvideos https://www.plurk.com/p/o48oz0 https://www.plurk.com/p/o48ort https://www.plurk.com/p/o48oyf https://www.plurk.com/p/o48oti https://www.plurk.com/p/o48owx https://www.plurk.com/p/o48ovi https://myanimelist.net/profile/streamvn https://www.ultimate-guitar.com/u/streamvnad https://gab.com/streamvnad https://www.awwwards.com/k-mr/favorites https://www.crunchyroll.com/user/mazda6pro https://www.blogtalkradio.com/videosxxx https://public.tableau.com/profile/anh.nguyen5988 https://checkio.org/user/streamvn.ad https://data.world/streamvnad http://www.cplusplus.com/user/streamvn/ https://www.longisland.com/profile/streamvnad https://discussion.evernote.com/profile/769950-streamvn/?tab=field_core_pfield_12 https://infogram.com/pornvideos-1h9j6qggv19yv4g?live https://streamvnad.newgrounds.com/ https://www.instructables.com/member/streamvn.ad https://dzone.com/users/4477044/streamvnad.html https://www.kiva.org/lender/anh7055 https://giphy.com/channel/streamvn https://www.codecademy.com/profiles/kMr3475010614 https://www.mobafire.com/profile/streamvn-973138?profilepage https://www.woddal.com/post/275798_https-www-kuxxx-com-categories-html-https-www-kuxxx-com-videos-top-https-www-kux.html https://www.woddal.com/streamvnad https://qiita.com/streamvnad https://www.discogs.com/user/streamvn.ad https://www.librarything.com/profile/streamvn https://www.seedandspark.com/user/anh-nguyen-10 https://mix.com/pornvideos https://www.turnkeylinux.org/user/1382994 https://app.lookbook.nu/user/8334964-Streamvn https://nootheme.com/forums/users/streamvn/ https://investimonials.com/users/streamvn.ad@gmail.com.aspx http://appsplit.com/users/streamvn https://profile.hatena.ne.jp/streamvnad/ https://stylowi.pl/streamvn/materialy https://www.diigo.com/profile/streamvnad http://diendan.hoidetmay.vn/members/streamvn-ad.94293/#about https://www.4shared.com/u/Cwxn_BKQ/streamvn.html https://moz.com/community/users/16725709 https://vi.gravatar.com/streamvnad http://www.bobscruises.com/user/anh-nguyen http://tupalo.com/en/users/2476560 https://blip.fm/streamvn https://www.mapleprimes.com/users/sreamvn https://www.zippyshare.com/streamvn https://challenges.openideo.com/profiles/streamvn.ad https://seekingalpha.com/user/52867311/comments https://angel.co/u/anh-nguyen-151 https://sumally.com/mo_nhac https://tune.pk/user/streamvn/about https://www.misterpoll.com/users/642886 https://www.producthunt.com/@k_mr https://bbpress.org/forums/profile/streamvn/ http://www.droidforums.net/members/anh-nguyen.433860/ https://thuvienso.khcnbackan.gov.vn/dien-dan/doc-gia/streamvn/ https://ioby.org/users/streamvnad429188 https://www.flicks.co.nz/member/streamvn.ad1395/ https://www.pearltrees.com/streamvn https://cults3d.com/fr/utilisateurs/streamvnad http://www.wikidot.com/user:info/streamvn https://www.patreon.com/pornvideos/ https://os.mbed.com/users/streamvnad/ https://pubhtml5.com/homepage/vkmm https://www.bonanza.com/users/46876837/profile https://play.eslgaming.com/player/16390454/ https://fliphtml5.com/homepage/cfcji https://torgi.gov.ru/forum/user/profile/1270692.page https://www.pokecommunity.com/member.php?u=911858 https://amara.org/en/profiles/profile/eeq0fkcacxzU3vf67PWvsXxZGh2IOg8MrHeLb-uVECQ/ https://player.me/streamvn/about https://streamvn.livejournal.com/profile http://www.mappery.com/user.php?name=streamvn https://forums.giantitp.com/member.php?235739-streamvn https://forum.cs-cart.com/user/108359-streamvn/ https://community.atlassian.com/t5/user/viewprofilepage/user-id/4188328 https://answers.informer.com/user/streamvn https://www.ted.com/profiles/25088416 https://microasp.upsc.se/streamvn https://gitlab.domainepublic.net/streamvn https://gitlab.pagedmedia.org/streamvn https://gitlab.coko.foundation/streamvn https://gitlab.redox-os.org/streamvn https://www.metooo.io/u/pornvideos https://www.webtoolhub.com/profile.aspx?user=42240740 https://forums.kleientertainment.com/profile/1332157-streamvn/?tab=field_core_pfield_16 http://sonicsquirrel.net/detail/user/streamvn/ https://www.indiegogo.com/individuals/25364939 https://www.atlasobscura.com/users/streamvnad https://list.ly/streamvn-ad/activity https://trello.com/anh754/activity https://lab.louiz.org/streamvn https://gitlab.kitware.com/streamvn https://hub.docker.com/u/streamvn https://hearthis.at/streamvn/ https://www.homify.vn/projects/780574/xxx-videos-porno?buy_article_banner=true https://www.homify.vn/professionals/8171765/streamvn https://pantip.com/profile/6217940#topics https://www.stem.org.uk/users/streamvn https://www.question2answer.org/qa/user/streamvn https://id.pr-cy.ru/user/profile/id/915474/# https://www.sbnation.com/users/SuBay-porn https://www.creativelive.com/student/porn-videos https://gamejolt.com/@pornvideos_a510 http://qooh.me/streamvn https://www.funnyordie.com/users/SuBay-porn https://www.blurb.com/user/streamvn?profile_preview=true https://www.redbubble.com/fr/people/streamvn https://www.gamespot.com/profile/streamvn/about-me/ http://kinhtexaydung.net/members/anh-nguyen-a.79675/ http://chuanmen.edu.vn/members/streamvn.10221/ http://hoiseotop1.com/members/streamvn.491574/ https://raovat49.com/s/ban-may-quay-videos-3148444 http://hoiseotop1.com/threads/bo-truong-quoc-phong-cac-nuoc-asean-ra-tuyen-bo-chung-17.298833/ https://bdsm-zone.com/member.php?u=44970 http://www.consolegames.ro/forum/members/streamvn/ https://3dprintboard.com/member.php?54903-streamvn https://www.zoimas.com/profile/streamvn/about https://ddld.vn/index.php?threads/khoa-van-tay-cua-nhom-an-toan-cho-gia-dinh-viet.48307/#post-76047 https://ddld.vn/index.php?members/streamvn.89624/#about https://forums.prosportsdaily.com/member.php?844698-streamvn http://www.webestools.com/profile-318948.html http://www.rohitab.com/discuss/user/116860-streamvn/ https://sailingforums.com/members/streamvn-pay.52762/#about https://homeservershow.com/profile/19050-streamvnpay/?tab=field_core_pfield_11 http://www.geekstogo.com/forum/user/431598-streamvnpay/ https://forum.lowyat.net/user/streamvnpay https://www.allkpop.com/forum/members/streamvnpay.193619/ https://www.kanyetothe.com/members/streamvn-pay.1736152/#about https://forum.topeleven.com/member.php?u=178803 https://forum.mikrotik.com/memberlist.php?mode=viewprofile&u=179380 https://mygicasupport.com/index.php?/profile/20067-streamvnpay/&tab=field_core_pfield_1

Đã trả lời thg 11 9, 2020 8:12 SA
+1

@dokkan7 Bạn có thể research thêm từ khóa regex, + cái muốn tìm ví dụ php regex start with @ chẳng hạn

Coding happy!

Chia sẻ
Avatar Dr Dokkan @dokkan7
thg 11 11, 2020 7:50 SA

Cảm ơn bạn.

thg 11 12, 2020 12:52 SA

@dokkan7 Bác giải quyết được vấn đề chưa thế

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí