I have a PHP Script that fetches text from the database, and what I want to do is the following:
If the text from the database looks like this:
[url="something"]Some text[/url]
I want it to look like this:
[url=something]Some text[/url]
I hope you can help me.
Thanks
You should use str_replace:
$str = str_replace('"','',$str);
$text = '[url="something"]Some text[/url]';
echo $text = preg_replace('#\[url="(.*?)"\]#i','[url=$1]', $text);
Related
I want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"
The text of story content in my database is:
I want to add\r\nnew line
(no quote)
When I use:
echo nl2br($story->getStoryContent());
to replace the \r\n with br, it doesn't work. The browser still display \r\n. When I view source, the \r\n is still there and br is nowhere to be found also. This is weird because when I test the function nl2br with simple code like:
echo nl2br("Welcome\r\nThis is my HTML document");
it does work. Would you please tell me why it didn't work? Thank you so much.
The following snippet uses a technique that you may like better, as follows:
<?php
$example = "\n\rSome Kind\r of \nText\n\n";
$replace = array("\r\n", "\n\r", "\r", "\n");
$subs = array("","","","");
$text = str_replace($replace, $subs, $example );
var_dump($text); // "Some Kind of Text"
Live demo here
I doubt that you need "\n\r" but I left it in just in case you feel it is really necessary.
This works by having an array of line termination strings to be replaced with an empty string in each case.
I found the answer is pretty simple. I simply use
$text = $this->storyContent;
$text = str_replace("\\r\\n","<br>",$text);
$text = str_replace("\\n\\r","<br>",$text);
$text = str_replace("\\r","<br>",$text);
$text = str_replace("\\n","<br>",$text);
I am basically trying to transform any hash-tagged word in a string into a link:
Here is what my code looks like:
public function linkify($text)
{
// ... generating $url
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/$1>#$1</a>", $text);
return $text;
}
It works pretty good excepting the case when that $text contains a single quote. Here are
Example1:
"What is your #name ?"
Result: "What is your #name?" Works fine.
Example2:
"What's your #name ?"
Result: "What's your #name?" Does not work, I want
this result: "What's your #name?"
Any idea about how I can get rid of that single quote problem using PHP ?
EDIT1:
Just for info, before or after html_entity_decode($text) I got
"What's your #name?"
Something like this.
$string = "' \'' '";
$string = preg_replace("#[\\\\']#", "\'", $string);
Something is protecting your html entities. This can save your life if the string is coming from a get/post request - but iI it's from a trusted source just use html_entity_decode to convert it back. This 39-thing is a way to express the single quote as you might have realized.
if the problem is html_entities, then maybe you only need to html_entity_decode your $text
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/html_entity_decode($1)>#$1</a>", $text);
Thanks all for your suggestions, I've finally sorted this out with this :
html_entity_decode($str, ENT_QUOTES);
what is the way to replace Spaces With + icon using PHP. Suppose some text like "I Love PHP" will automatically converted like this way "I+Love+PHP" .. assuming a field has this text in a variable like> $text = I Love PHP so this variables text spaces will be replace with a + sign in a new variable like this> $text_plus=I+Love+PHP how to do it with PHP?
Just a guess, but this looks like you're trying to encode the string for a URL, use urlencode().
Using Str_Replace function
In your case it will be :
$text = "I Love PHP";
$output = str_replace(" ", "+", $text);
Try This
str_ireplace(' ','+',$text);
I want to parse a html content that have something like this:
<td class="s3_40">12,909</td>
i tried many regex to find string in between (12,909)
like : %<td class=\"s3_40\">(.*)</td>%
but i didn't find that .
Hope this can help:
$str = '<td class="s3_40">12,909</td>';
if (preg_match('#<td class="s3_40">(.*)</td>#s', $str, $matches)) {
var_dump($matches[1]);
}
If you must do it with a regex. You can try a pattern like this:
/<td[^>]*>(.*?)<\/td>/
Try this
#(?<=(s3_40">))([^<]+)(?=(</td>))#
Try doing it like this, just keep adding to s3_41, s3_42, etc to the class you need.
<?php
$s = '<td class="s3_40">12,909</td>';
$pat = '#<td class=".*(s3_40|s3_41).*".*?>(.*?)</td>#isU';
preg_match_all($pat,$s,$ms);
var_dump($ms);