I have a problem with my code, i have this code that create image from external source of image & string. I used json to get the string.
My problem is if i used the string from json data i could not get the proper wrapping of string like this:
http://prntscr.com/dbhg4n
$url = 'https://bible-api.com/Psalm100:4-5?translation=kjv';
$JSON = file_get_contents($url);
$data = json_decode($JSON);
$string = $data->text;
But if i declare and set string directly i got the output that i want like this:
http://prntscr.com/dbhg7q
$string = "Enter into his gates with thanksgiving, and into his courts with praise: be thankful unto him, and bless his name. For the Lord is good; his mercy is everlasting; and his truth endureth to all generations.";
I dont think the error or the problem is on the code for wrapping the text on my image. I think it is on the json data. How can i fix this?
The text has \n symblols. Just replace them:
$string = preg_replace("/\n/", ' ', $data->text);
or without a regular expression:
$string = str_replace("\n", ' ', $data->text);
Related
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);
When I try
$stg = preg_replace("/<PREPNAME>.*<\/PREP_ZIP>/s","",$stg);
It properly work and it replace the text written in that function. And I get $stg is stripped and I get remaining string.
But when I try with "\n", it empty the complete string.
$stg = preg_replace("/<PREPNAME>.*<\/PREP_ZIP>\n/s","",$stg);
I get $stg empty in second case.
Please help. Where I am missing ?
Example String :-
This is not an XML string. This is just a blob data that is stored in the database. While fetching this is just a string.
<T2_NASD_LAST_NAME1>ASDSADSA</T2_NASD_LAST_NAME1>\r\n
<T2_NASD_FIRST_NAME1>SADASD</T2_NASD_FIRST_NAME1>\r\n
<T2_SOC1>012345678</T2_SOC1>\r\n
<T2_DOBM1>02</T2_DOBM1>\r\n <T2_DOBD1>02</T2_DOBD1>\r\n
<T2_DOBY1>1984</T2_DOBY1>\r\n
<T2_NASD_LAST_NAME>ASDSADSA</T2_NASD_LAST_NAME>\r\n
<T2_NASD_FIRST_NAME>SADASD
</T2_NASD_FIRST_NAME>\r\n
<T2_NASD_MIDDLE_NAME>ASDA</T2_NASD_MIDDLE_NAME>\r\n
<T2_SOC>012345678</T2_SOC>\r\n<T2_ADR_ADRN>SADSADAS</T2_ADR_ADRN>\r\n
<T2_ADR_ADRA>DASD</T2_ADR_ADRA>\r\n<T2_ADR_ADRC>ASDSADSA</T2_ADR_ADRC>\r\n
<T2_ADR_ADRS>MI</T2_ADR_ADRS>\r\n<T2_ADR_ADRZ>11111</T2_ADR_ADRZ>\r\n
<T2_DOBM>02</T2_DOBM>\r\n<T2_DOBD>02</T2_DOBD>\r\n
<T2_DOBY>1984</T2_DOBY>\r\n\n\n\n\n\n\n\n<PREPFLAG>NONE</PREPFLAG>\n
<PREPNAME>AAAA</PREPNAME>\n
<PREPADDRESS>BBBB</PREPADDRESS>\n
<PREP_ZIP>1984</PREP_ZIP>
You must add two \ before \n and use simple quotes
<?php
$subject = <<<'LOD'
<T2_DOBY1>1984</T2_DOBY1>\r\n
<T2_NASD_LAST_NAME>ASDSADSA</T2_NASD_LAST_NAME>\r\n
<T2_NASD_FIRST_NAME>SADASD
<PREP_ZIP>NONE</PREP_ZIP>\n
</T2_NASD_FIRST_NAME>\r\n
<T2_DOBY1>1984</T2_DOBY1>\r\n
<T2_NASD_LAST_NAME>ASDSADSA</T2_NASD_LAST_NAME>\r\n
<T2_NASD_FIRST_NAME>SADASD
</T2_NASD_FIRST_NAME>\r\n
LOD;
$pattern = '~<PREP_ZIP>.*?</PREP_ZIP>\\\n~s';
echo htmlspecialchars(preg_replace($pattern, "#!!!YOUHOU!!!#", $subject));;
I'm trying to scrape a website using some regex. But the site isn't written in well formatted html. In fact, the html is horrible and not structured hardly at all. But I've managed to tackle most of it. The problem I'm encountering now is that in some emails, a span is wrapped around a random part of the email like so:
****.*******#g<span class="tournamenttext">mail.com</span>
************<span class="tournamenttext">#yahoo.com</span>
<span class="tournamenttext">**********#mail.com</span>
*******#gmail.com
Is there a way to retrieve the emails with all this inconsistency?
$string ='****.*******#g<span class="tournamenttext">mail.com</span>
************<span class="tournamenttext">#yahoo.com</span>
<span class="tournamenttext">**********#mail.com</span>
*******#gmail.com';
$pattern = "/<\/?span[^>]*>/";
$string = preg_replace($pattern, "", $string);
after that $string will be only mails
****.*******#gmail.com
************#yahoo.com
**********#mail.com
*******#gmail.com
Your code will be like this
$text[1]->innertext = "Where innertext contains something like: "<em>Local (Open)
Tournament.</em> ****.*******#g<span class="tournamenttext">mail.com</span>"
// Firstly clear spans
$pattern = "/<\/?span[^>]*>/";
$text[1]->innertext = preg_replace($pattern, "", $text[1]->innertext);
// Preg Match mail
$email_regex = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; // Just an example email match regex
preg_match($email_regex, $text[1]->innertext, $theMatch);
echo '<pre>' . print_r($theMatch, true) . '</pre>';
You could simply remove all span tags by replacing </?span[^>]*> with nothing and try your favourite email address finder on the result.
I'm trying to use preg_replace to strip out a section of code but I am having problems getting it to work right.
Code Example:
$str = '<p class="code">some string here</p>';
PHP I'm using:
$pattern = array();
$pattern[0] = '!<p class="code">!';
$pattern[1] = '!</p>!';
preg_replace($pattern,"", $str);
This strips out the code just as I want with the exception of the space between the p and class.
Returns:
some string here //notice the single space at the beginning.
I'm trying to get:
some string here //no space at the beginning.
I have been beating my head against the wall trying to find a solution. The reason I'm trying to strip it out in a chunk instead of breaking the preg_replace into pieces is because I don't want to change anything that may be in the string between the tags. Any ideas?
That does not happen for me (and it shouldn't).
It may be a space output somewhere else (use var_dump() to view the string).
You might want to look into this thread to see if you want to switch to using DOMDocument. It'll save you a great deal of headaches trying to parse through HTML.
Robust and Mature HTML Parser for PHP
test:
<?php
$str = '<p class="code">some string here</p>';
$pattern = array();
$pattern[0] = '!<p class="code">!';
$pattern[1] = '!</p>!';
$result = preg_replace($pattern,"", $str);
var_dump($result);
result:
php pregrep.php
string(16) "some string here"
seems to work just fine.
Alex I figured out where I was picking up the extra space.
I was putting that code into a text area like this:
$str = '<p class="code">some string here</p>';
$pattern = array();
$pattern[0] = '!<p class="code">!';
$pattern[1] = '!</p>!';
$strip_str = preg_replace($pattern,"", $str);
<textarea id="code_area" class="syntaxhl" name="code" cols="66" rows="5">
<?php echo $strip_str; ?>
</textarea>
This gave me my extra space but when I changed the code to:
<textarea id="code_area" class="syntaxhl" name="code" cols="66" rows="5"><?php echo $strip_str; ?></textarea>
No line spaces or breaks the extra space went away.
Why not use trim()?
$text = trim($text);
This removes white spaces around strings.