String Replace UFT-8 and ApphaNumeric In php? - php

Input string:
$string = "Dinesh G தினேஷ் ";
I Would Like to Convert this string into
<span class="english">Dinesh</span> <span class="english">G</span> தினேஷ்
Please help me resolve this issue.

Try this simplest code, It will check for characters A-Z , a-z then we capture those words and replace them with span tags around it.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string = "Dinesh G தினேஷ் ";
echo preg_replace("/([A-Za-z]+)/", '<span class="english">$1</span>', $string);
Output:
<span class="english">Dinesh</span> <span class="english">G</span> தினேஷ்

Related

str_replace with content not replacing

Hello Stackers,
I'm just having a small PHP Question about the str_replace() Function. When I replace something, it will just replace everything; That's okay. But what I would like to know is this:
str_replace("*", "<strong>", $message);
Is it possible to use str_replace for codes like * This content is Bold *, just having the content, but still replacing the asterisk's with <strong> and </strong>?
Example:
Original: **This Should be Bold**
After Replacing: <strong>This Should be Bold</strong>
For people flagging this as a Duplicate: It's not about closing HTML Tags, it's about replacing.
I Hope I'm not that unclear. Thanks.
Use regular expression instead; it's more convenient:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong>$1</strong>', $message);
echo $message;
Or if you want to limit the number of asteroids to 2:
'#\*{1,2}([^\*]+)\*{1,2}#m'
You can also do like this
https://eval.in/518881
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong>$1</strong>", $string);
echo $string;
?>

Remove special characters like lt; but not anchor tag

How can I remove special characters like ;lt ;gt but not Anchor tag
e.g
&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
should be
Spike Jonze This cause by <a class="primary-black" href="http://example.com/community/RobHallums">RobHallums</a>
Here's a quick one for you:
<?php
// SET OUR DEFAULT STRING
$string = '&lt;a href=&quot;http://w...content-available-to-author-only...b.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt; This cause by <a class="primary-black" href="http://e...content-available-to-author-only...e.com/community/RobHallums">RobHallums</a>';
// USE PREG_REPLACE TO STRIP OUT THE STUFF WE DON'T WANT
$string = preg_replace('~&lt;.*?&gt;~', '', $string);
// PRINT OUT OUR NEW STRING
print $string;
All I'm doing here is looking for &lt;, followed by any character ., any number of times *, until it matches the next part of the string ?, which is &gt;.
Any time it finds that, it replaces it with nothing. So you're left with the text you want.
Here is a working demo:
http://ideone.com/uSnY0b
use html_entity_decode:
<?php $url = html_entity_decode('&lt;a href=&quot;http://www.imdb.com/name/nm0005069/&quot;&gt;Spike Jonze&lt;/a&gt;');
echo $url;
?>
the output will be:
Spike Jonze
EDIT:
<?php
preg_match_all('/<a .*?>(.*?)<\/a>/',$url,$matches);
//For Text Name
echo $matches[1][0]; //output : Spike Jonze
?>

how to regex character < and > replace like < and > in tag <code> </code>?

I have a string like bellow:
<pre title="language-markup">
<code>
<div title="item_content item_view_content" itemprop="articleBody">
abc
</div>
</code>
</pre>
In the <code></code> tag I want to replace all the characters < and > with < and >. How should I do?
Example: <code> < div ><code>.
Please tell me if you have any ideas. Thanks all.
try below solution:
$textToScan = '<pre title="language-markup">
<code>
<div title="item_content item_view_content" itemprop="articleBody">
abc
</div>
</code>
</pre>';
// the regex pattern (case insensitive & multiline
$search = "~<code>(.*?)</code>~is";
// first look for all CODE tags and their content
preg_match_all($search, $textToScan, $matches);
//print_r($matches);
// now replace all the CODE tags and their content with a htmlspecialchars() content
foreach($matches[1] as $match){
$replace = htmlspecialchars($match);
// now replace the previously found CODE block
$textToScan = str_replace($match, $replace, $textToScan);
}
// output result
echo $textToScan;
output:
<pre title="language-markup">
<code>
<div title="item_content item_view_content" itemprop="articleBody">
abc
</div>
</code>
</pre>
Don't. Use htmlspecialchars. That is there only to serve that very purpose
echo htmlspecialchars("<a href='test'>Test</a>");
Output of your HTML code
<pre title="language-markup"><code>
<div title="item_content item_view_content"
itemprop="articleBody">abc</div></code></pre>
Another example based on your comment
<code>
<?php
echo htmlspecialchars('html here');?>
</code>
Use either htmlspecialchars() or htmlentities()
$string = "<html></html>"
// Do this
$encodedString = htmlentities($string);
// or
$encodedString = htmlspecialchars($string);
The difference in these two functions is that one will encode everything or better said "entities". The other will only encode special characters.
Below are some quotes from PHP.net
From the PHP documentation for htmlentities:
This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
From the PHP documentation for htmlspecialchars:
Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.
Ok, I'm trying to fix my problem. I was successed, this is my code to resolve my problem. You can use my way or use Chetan Ameta's way bellow my answer:
function replaceString($string)
{
preg_match_all('/<code>(.*?)<\/code>/', $string, $matches);
$result = [];
foreach ($matches[1] as $key => $match) {
$result[$key] = str_replace(['<', '>'], ['<', '>'], $match);
}
return str_replace($matches[1], $result, $string);
}
$string = '<pre title="language-markup"><code><div title="item_content item_view_content" itemprop="articleBody">abc</div></code></pre>';
echo replaceString($string);
I like this place, thanks all help me, i'm so grateful. Thank again.

Replace html tag with class and var php

I want to replace a string that I get from database. I want to replace span by an empty string.
My string is like so:
<p><span class="coursehours">x horas lectivas</span>
<span class="coursehours">x horas prácticas</span></p>
X is a number and I want to remove the p tag entire.
How do I should do?
I think this should work too?
var text = "<p> <span class='coursehours'>x horas lectivas</span> <span class='coursehours'>x horas prácticas</span></p>";
var str = text.trim().substring(3, text.length - 4);
alert(str.trim());
Sorry because I have explained wrong
What I wanted to eliminate only those span
Seems with this is fixed
preg_replace('#<span class="coursehours">(.*?)</span>#', '', $string);
Thanks to all !
Try stripping HTML and PHP tags from a string
http://php.net/manual/en/function.strip-tags.php
Something like this:
$str = "your string";
$str = strip_tags($str);

Add Brackets to Timestamp

I'm new to PHP so I don't know whether this is possible.
I need to add brackets to different timestamps so that this:
<span class="time">2:26</span>
<span class="time">2:51</span>
<span class="time">3:37</span>
<span class="time">1:19</span>
becomes this:
<span class="time">(2:26)</span>
<span class="time">(2:51)</span>
<span class="time">(3:37)</span>
<span class="time">(1:19)</span>
EDIT
The above HTML is generated using a simple DOM parser to grab info from a webpage.
If this is part of a larger HTML string or if the syntax might differ, it's a better idea to use a DOM parser.
However, if that isn't the case you can do this:
$string = str_replace('<span class="time">', '<span class="time">(', $string);
$string = str_replace('</span>', ')</span>', $string);
Or you can use regex:
$string = preg_replace('/<span class=\"time\">(\d+\:\d+)<\/span>/', '<span class="time">($1)</span>', $string);
Assuming your timestamp string is in $timestamp variable, you could use concatenation, ie.:
$output = '(' . $timestamp . ')';
Or as you mentioned, using a regular expression to validate the string before addind brackets:
$output = preg_replace("/(\d+):(\d+)/", "($0)", $timestamp);

Categories