@@ -129,6 +129,30 @@ class Autolink
129129 */
130130 protected $ invisibleTagAttrs = "style='position:absolute;left:-9999px;' " ;
131131
132+ /**
133+ * If the at mark '@' should be included in the link (false by default)
134+ *
135+ * @var bool
136+ * @since 3.0.1
137+ */
138+ protected $ usernameIncludeSymbol = false ;
139+
140+ /**
141+ * HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
142+ *
143+ * @var string
144+ * @since 3.0.1
145+ */
146+ protected $ symbolTag = '' ;
147+
148+ /**
149+ * HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
150+ *
151+ * @var string
152+ * @since 3.0.1
153+ */
154+ protected $ textWithSymbolTag = '' ;
155+
132156 /**
133157 *
134158 * @var Extractor
@@ -387,6 +411,75 @@ public function setTarget($v)
387411 return $ this ;
388412 }
389413
414+ /**
415+ * @return bool
416+ * @since 3.0.1
417+ */
418+ public function isUsernameIncludeSymbol ()
419+ {
420+ return $ this ->usernameIncludeSymbol ;
421+ }
422+
423+ /**
424+ * Set if the at mark '@' should be included in the link (false by default)
425+ *
426+ * @param bool $usernameIncludeSymbol if username includes symbol
427+ * @return Autolink
428+ * @since 3.0.1
429+ */
430+ public function setUsernameIncludeSymbol ($ usernameIncludeSymbol )
431+ {
432+ $ this ->usernameIncludeSymbol = $ usernameIncludeSymbol ;
433+
434+ return $ this ;
435+ }
436+
437+ /**
438+ * @return string
439+ * @since 3.0.1
440+ */
441+ public function getSymbolTag ()
442+ {
443+ return $ this ->symbolTag ;
444+ }
445+
446+ /**
447+ * Set HTML tag to be applied around #/@/# symbols in hashtags/usernames/lists/cashtag
448+ *
449+ * @param string $symbolTag HTML tag without bracket. e.g., 'b' or 's'
450+ * @return Autolink
451+ * @since 3.0.1
452+ */
453+ public function setSymbolTag ($ symbolTag )
454+ {
455+ $ this ->symbolTag = $ symbolTag ;
456+
457+ return $ this ;
458+ }
459+
460+ /**
461+ * @return string
462+ * @since 3.0.1
463+ */
464+ public function getTextWithSymbolTag ()
465+ {
466+ return $ this ->textWithSymbolTag ;
467+ }
468+
469+ /**
470+ * Set HTML tag to be applied around text part of hashtags/usernames/lists/cashtag
471+ *
472+ * @param string $textWithSymbolTag HTML tag without bracket. e.g., 'b' or 's'
473+ * @return Autolink
474+ * @since 3.0.1
475+ */
476+ public function setTextWithSymbolTag ($ textWithSymbolTag )
477+ {
478+ $ this ->textWithSymbolTag = $ textWithSymbolTag ;
479+
480+ return $ this ;
481+ }
482+
390483 /**
391484 * Autolink with entities
392485 *
@@ -404,18 +497,14 @@ public function autoLinkEntities($tweet = null, $entities = null)
404497 $ text = '' ;
405498 $ beginIndex = 0 ;
406499 foreach ($ entities as $ entity ) {
407- if (isset ($ entity ['screen_name ' ])) {
408- $ text .= StringUtils::substr ($ tweet , $ beginIndex , $ entity ['indices ' ][0 ] - $ beginIndex + 1 );
409- } else {
410- $ text .= StringUtils::substr ($ tweet , $ beginIndex , $ entity ['indices ' ][0 ] - $ beginIndex );
411- }
500+ $ text .= StringUtils::substr ($ tweet , $ beginIndex , $ entity ['indices ' ][0 ] - $ beginIndex );
412501
413502 if (isset ($ entity ['url ' ])) {
414503 $ text .= $ this ->linkToUrl ($ entity );
415504 } elseif (isset ($ entity ['hashtag ' ])) {
416505 $ text .= $ this ->linkToHashtag ($ entity , $ tweet );
417506 } elseif (isset ($ entity ['screen_name ' ])) {
418- $ text .= $ this ->linkToMentionAndList ($ entity );
507+ $ text .= $ this ->linkToMentionAndList ($ entity, $ tweet );
419508 } elseif (isset ($ entity ['cashtag ' ])) {
420509 $ text .= $ this ->linkToCashtag ($ entity , $ tweet );
421510 }
@@ -651,7 +740,7 @@ public function linkToHashtag($entity, $tweet = null)
651740 $ attributes = array ();
652741 $ class = array ();
653742 $ hash = StringUtils::substr ($ tweet , $ entity ['indices ' ][0 ], 1 );
654- $ linkText = $ hash . $ entity ['hashtag ' ];
743+ $ linkText = $ entity ['hashtag ' ];
655744
656745 $ attributes ['href ' ] = $ this ->url_base_hash . $ entity ['hashtag ' ];
657746 $ attributes ['title ' ] = '# ' . $ entity ['hashtag ' ];
@@ -665,18 +754,20 @@ public function linkToHashtag($entity, $tweet = null)
665754 $ attributes ['class ' ] = implode (' ' , $ class );
666755 }
667756
668- return $ this ->linkToText ($ entity , $ linkText , $ attributes );
757+ return $ this ->linkToTextWithSymbol ($ entity, $ hash , $ linkText , $ attributes );
669758 }
670759
671760 /**
672761 *
673762 * @param array $entity
763+ * @param string $tweet
674764 * @return string
675765 * @since 1.1.0
676766 */
677- public function linkToMentionAndList ($ entity )
767+ public function linkToMentionAndList ($ entity, $ tweet )
678768 {
679769 $ attributes = array ();
770+ $ symbol = StringUtils::substr ($ tweet , $ entity ['indices ' ][0 ], 1 );
680771
681772 if (!empty ($ entity ['list_slug ' ])) {
682773 # Replace the list and username
@@ -694,7 +785,7 @@ public function linkToMentionAndList($entity)
694785 }
695786 $ attributes ['href ' ] = $ url ;
696787
697- return $ this ->linkToText ($ entity , $ linkText , $ attributes );
788+ return $ this ->linkToTextWithSymbol ($ entity, $ symbol , $ linkText , $ attributes );
698789 }
699790
700791 /**
@@ -710,15 +801,15 @@ public function linkToCashtag($entity, $tweet = null)
710801 $ tweet = $ this ->tweet ;
711802 }
712803 $ attributes = array ();
713- $ doller = StringUtils::substr ($ tweet , $ entity ['indices ' ][0 ], 1 );
714- $ linkText = $ doller . $ entity ['cashtag ' ];
804+ $ dollar = StringUtils::substr ($ tweet , $ entity ['indices ' ][0 ], 1 );
805+ $ linkText = $ entity ['cashtag ' ];
715806 $ attributes ['href ' ] = $ this ->url_base_cash . $ entity ['cashtag ' ];
716- $ attributes ['title ' ] = $ linkText ;
807+ $ attributes ['title ' ] = ' $ ' . $ linkText ;
717808 if (!empty ($ this ->class_cash )) {
718809 $ attributes ['class ' ] = $ this ->class_cash ;
719810 }
720811
721- return $ this ->linkToText ($ entity , $ linkText , $ attributes );
812+ return $ this ->linkToTextWithSymbol ($ entity, $ dollar , $ linkText , $ attributes );
722813 }
723814
724815 /**
@@ -752,6 +843,35 @@ public function linkToText(array $entity, $text, $attributes = array())
752843 return $ link ;
753844 }
754845
846+ /**
847+ *
848+ * @param array $entity
849+ * @param string $symbol
850+ * @param string $linkText
851+ * @param array $attributes
852+ * @return string
853+ * @since 3.0.1
854+ */
855+ protected function linkToTextWithSymbol (array $ entity , $ symbol , $ linkText , array $ attributes )
856+ {
857+ $ includeSymbol = $ this ->usernameIncludeSymbol || !preg_match ('/[@@]/u ' , $ symbol );
858+
859+ if (!empty ($ this ->symbolTag )) {
860+ $ symbol = sprintf ('<%1$s>%2$s</%1$s> ' , $ this ->symbolTag , $ symbol );
861+ }
862+ if (!empty ($ this ->textWithSymbolTag )) {
863+ $ linkText = sprintf ('<%1$s>%2$s</%1$s> ' , $ this ->textWithSymbolTag , $ linkText );
864+ }
865+
866+ if (!$ includeSymbol ) {
867+ return $ symbol . $ this ->linkToText ($ entity , $ linkText , $ attributes );
868+ }
869+
870+ $ linkText = $ symbol . $ linkText ;
871+
872+ return $ this ->linkToText ($ entity , $ linkText , $ attributes );
873+ }
874+
755875 /**
756876 * html escape
757877 *
0 commit comments