سؤال

أريد استبدال علامات التجزئة في سلسلة بنفس علامة التجزئة، ولكن بعد إضافة رابط إليها

مثال:

$text = "any word here related to #English must #be replaced."

أريد استبدال كل هاشتاج بـ

#English ---> <a href="bla bla">#English</a>
#be ---> <a href="bla bla">#be</a>

لذا يجب أن يكون الإخراج هكذا:

$text = "any word here related to <a href="bla bla">#English</a> must <a href="bla bla">#be</a> replaced."
هل كانت مفيدة؟

المحلول

giveacodicetagpre.

تجريبي

الإخراج :

giveacodicetagpre.

نصائح أخرى

يجب أن يدفعك هذا في الاتجاه الصحيح:

echo preg_replace_callback('/#(\w+)/', function($match) {
    return sprintf('<a href="https://www.google.com?q=%s">%s</a>', 
        urlencode($match[1]), 
        htmlspecialchars($match[0])
    );
}, htmlspecialchars($text));

أنظر أيضا: preg_replace_callback()

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top