PHP: make a string clickable - php

For a Mandarin learning-tool I would like to create links for each chinese "character" within a word. For example I have the chinese word "自行车" (bicycle). Then I would like to make each of the three characters "clickable".
$word = '自行车';
And the output should be:
$output = "<a href='?char=自'>自</a>
<a href='?char=行'>行</a>
<a href='?char=车'>车</a>";
Does anyone have an idea how to do this?

With this regex you can split your characters.
preg_split('//u', "自行车", null, PREG_SPLIT_NO_EMPTY);
the result is array of three character.

You can use preg_replace to replace the chinese char with regular expression /(\p{Han})/u then replace it with what your need.
preg_replace("/(\p{Han})/u","<a href='?char=$1'>$1</a>",'ss自行车ss');
output:
ss<a href='?char=自'>自</a><a href='?char=行'>行</a><a href='?char=车'>车</a>ss
Refer to Php - regular expression to check if the string has chinese chars

You can extract chars in a for loop and generate anchor tags.
<a href='<link?char=<extracted char>'><extracted char></a>

Related

Replace string at particular position In PHP?

I have to replace string but it's simple in PHP but my string just like these here i show you.Please any one help me.
$string = "#x93F;#x902;#x91C";
Above string i want to replace it with
#x91C;#x93F;#x902;
But one thing in these string replace. We don't know last word of the $string #x91C; .
Any word comes in last it's place in to front of that string. How can i solve that please any one help me.
Use capturing groups to capture the characters you want. Later you could replace the matched characters with the chars inside the group.
Regex:
^([^;]*);([^;]*);([^;]*);$
Replacement string:
$3;$1;$2;
DEMO
$string = "#x93F;#x902;#x91C;";
echo preg_replace('~^([^;]*);([^;]*);([^;]*);$~', '$3;$1;$2;', $string);
Output:
#x91C;#x93F;#x902;
((?:[^;]+;)*)([^;]+)(?=$)
Replace by $2;$1.
See demo.
http://regex101.com/r/uH3tP3/9

PHP converting plain text to hashtag link

I am trying to convert user's posts (text) into hashtag clickable links, using PHP.
From what I found, hashtags should only contain alpha-numeric characters.
$text = 'Testing#one #two #three.test';
$text = preg_replace('/#([0-9a-zA-Z]+)/i', '#$1', $text);
It places links on all (#one #two #three), but I think the #one should not be converted, because it is next to another alpha-numeric character, how to adjust the reg-ex to fix that ?
The 3rd one is also OK, it matches just #three, which I think is correct.
You could modify your regex to include a negative lookbehind for a non-whitespace character, like so:
(?<!\S)#([0-9a-zA-Z]+)
Working regex example:
http://regex101.com/r/mR4jZ7
PHP:
$text = preg_replace('/(?<!\S)#([0-9a-zA-Z]+)/', '#$1', $text);
Edit:
And to make the expression compatible with other languages (non-english characters):
(?<!\S)#([0-9\p{L}]+)
Working example:
https://regex101.com/r/Pquem3/1
With uni-code, html encoded safe and joined regexp; ~(?<!&)#([\pL\d]+)~u
Here some's tags like #tag1 #tag2#tag3 etc.
Finally I have found the solution like: facebook or others hashtag to url solutions, it may be help you too. This code also works with unicode. I have used some of Bangla Unicode, let me know other Languages work as well, I think it will work on any language.
$str = '#Your Text #Unicode #ফ্রিকেলস বা #তিল মেলানিনের #অতিরিক্ত উৎপাদনের জন‍্য হয় যা #সূর্যালোকে #বাড়ে';
$regex = '/(?<!\S)#([0-9a-zA-Z\p{L}\p{M}]+)/mu';
$text = preg_replace($regex, '#$1', $str);
echo $text;
To catch the second and third hashtags without the first one, you need to specify that the hashtag should start at the beginning of the line, or be preceded one of more characters of whitespace as follows:
$text = 'Testing#one #two #three.test';
$text = preg_replace('/(^|\s+)#([0-9a-zA-Z]+)(\b|$)/', '$1#$2', $text);
The \b in the third group defines a word boundary, which allows the pattern to match #three when it is immediately followed by a non-word character.
Edit: MElliott's answer above is more efficient, for the record.

i want to exclude the content inside the html tags when using str_ireplace

I have this as a sample text
......
$text = "Swiss Real";
......
now what i do is this
$text = str_ireplace('swiss','<font color="red">swiss</font>',$text);
now my string is
$text is '<font color="red">swiss</font> Real'
now i want to do
$text = str_ireplace('re','<font color="red">re</font>',$text);
now here is my problem... i want to change only 'Real' and not the re in the 'color="red"'
how do i achieve it.
please help me.
You need to use boundary
\bre\b
With boundary you can match characters that are surrounded by non-word characters..
Simply put it \b around a word enables you to match an individual word
For example for string "Begin it in stackoverflow" and if you want to replace in with IN
in regex would replace it to "BegIN it IN stackoverflow"
\bin\b regex would replace it to "Begin it IN stackoverflow"

Convert links in string with non-latin chars using regex

I was using this function to find links in a string and convert them to html links
function makeClickableLinks($s) {
return preg_replace('#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#', '$1', $s);
}
The problem is that its not working with urls with non-latin chars like this
https://www.facebook.com/pages/Celebração/123434584839
for which the result is
https://www.facebook.com/pages/Celebra��ão/123434584839
Any help?
Try to use regex pattern
(?:(^)|(?<=(.)))((?<!^)https?://.*?(?=\1)|https?://.*?(?=\s|$))
having url in $2
To match latin characters you should be using unicode friendly regex. Something like this should work:
#(https?://([-\pL\.]+[-\pL])+(:\pN+)?(/([\pL/_\.#-]*(\?\S+)?[^\.\s])?)?)#u

how to extract a portion of a string in php

I am using preg_replace() for some string replacement.
$str = "<aa>Let's find the stuff qwe in between <id>12345</id> these two previous brackets</h>";
$do = preg_match("/qwe(.*)12345/", $str, $matches);
which is working just fine and gives the following result
$match[0]=qwe in between 12345
$match[1]=in between
but I am using same logic to extract from the following string.
<text>
<src><![CDATA[<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="36" COLOR="#999999" LETTERSPACING="0" KERNING="0">r1 text 1 </FONT></P></TEXTFORMAT>]]></src>
<width>45%</width>
<height>12%</height>
<left>30.416666666666668%</left>
<top>3.0416666666666665%</top>
<begin>2s</begin>
<dur>10s</dur>
<transIn>fadeIn</transIn>
<transOut>fadeOut</transOut>
<id>E2159292994B083ACA7ABC7799BBEF3F7198FFA2</id>
</text>
I want to extract the string from
r1text1
to
</id>
The Regular expression I currently Have is:
preg_match('/r1text1(.*)</id\>/', $metadata], $matches);
where $metadata is the above string..
$matches does not return anything....
For some reason...how do i do it?
Thanks in advance
If you want to extract the text, you will probably want to use preg_match. The following might work:
preg_match('#\<P[^\>]*\>\<FONT[^\>]*\>(.*\</id\>)#', $string, $matches)
Whatever gets matched in the parantheses can be found later in the $matches array. In this case everything between a <P> tag followed by a <FONT> tag and </id>, including the latter.
Above regex is untested but might give you a general idea of how to do it. Adapt if your needs are a bit different :)
Even if don't know why you would match the regex on a incomplete XML fragment (starting within a <![CDATA[ and ending right before the closing XML tag </id>, you do have three obvious problems with your regex:
As Amri said: you have to escape the / character in the closing XML tag because you use / as the pattern delimiter. By the way, you don't have to escape the > character. That gives you: '/r1text1(.*)<\/id>/' Alternatively you can change the pattern delimiter to # for example: '#r1text1(.*)</id>#' (I will use the first pattern to further develop the expression).
As Rich Adams already said: the text in your example data is "r1_text_1" (_ is a space character) but you match against '/r1text1(.*)<\/id>/'. You have to include the spaces in your regex or allow for a uncertain number of spaces, such as '/r1(?:\s*)text(?:\s*)1(.*)<\/id>/' (the ?: is the syntax for non-capturing subpatterns)
The . (dot) in your regex does not match newlines by default. You have to add the s (PCRE_DOTALL) pattern modifier to let the . (dot) match against newlines as well: '/r1(?:\s*)text(?:\s*)1(.*)<\/id>/s'
you probably need to parse your string/file and extract the value between the FONT tag. Then insert the value into the id tag
Try googling for php parsing.
try this
preg_match('/r1text1(.*)<\/id\>/', $metadata], $matches);
You are using / as the pattern delimiter but your content has / in . You can use \ as the escape character.
In the sample you have "r1 text 1 ", yet your regular expression has "r1text1". The regular expression doesn't match because there are spaces in the string you are trying to match it against. You should include the spaces in the regular expression.

Categories