I'm processing each commit by using:
$author = $commit['author']['raw'];
$commitMessage = $commit['message'];
$commitMessage = trim(str_replace("\n", "", $commitMessage));
$date = str_replace('T', ' ', $commit['date']) . "\n";
$date = explode("+", $date);
$date = $date[0];
$message = $author . "\n" . $commitMessage . "\n" . $date;
And I get """ prepended and appended to the array, so:
"""
some data
"""
Does anyone have any idea on how to remove it?
I've tried using str_replace with different needles, also tried using strpos to see if it even is detected.
Using explode doesn't help either...
I fixed it...
I was using dd() (since I was just testing the output of the array), and I switched just now by returning it as a normal array.
Related
How can i prevent that PHP converts a recognized part of a string to an html-entity?
So e.g. lets say i have to concat parts together to an url, like:
echo '&' . 'section=' . '<br>';
$a = '&a';
$b = 'mplitude=';
echo "{$a}{$b}" . '<br>';
echo sprintf("%s%s", '"e', '=');
the code above prints the following:
§ion=
&litude=
"e=
instead of:
§ion=
&litude=
"e=
how can this be prevented without throwing filters on it trying to convert the symbols back to an string again?
You need using htmlspecialchars function:
echo htmlspecialchars('&' . 'section=' . '<br>');
<?php
echo "I'm currently listening to</a> <a href='http://last.fm/artist/" . str_replace(" ","+",$artist) . "/_/" . str_replace(" ","+",$currenttrack) . "'>" . $currenttrack . "</a>";
?>
Above is my code. I'm trying to use str_replace() again on $artist and $currenttrack like:
str_replace("'","%27",$artist) and str_replace("'","%27",$currenttrack)
because the apostrophe doesn't go through correctly and messes with my code, but when I use it first with the spaces, it's already passed and won't change again.
What can I do?
If you want to do multiple replacements on the same string, you can pass arrays to str_replace:
str_replace(array(" ", "'"), array("+", "%27"), $artist)
However, when creating URL parameters, you shouldn't do the replacements yourself. You should use urlencode, and it will do all the necessary encodings.
Try this. It also makes your code more readable. Also, your anchor tags aren't formatted correctly.
$artist = str_replace(' ', '+', $artist);
$track = str_replace(' ', '%27', $currenttrack);
echo 'I\'m currently listening to ' . $track . '';
If I understood your question correctly, you are trying to replace spaces by + and ' by %27 in the two strings. To achieve this, you have to apply str_replace() on the result of the first operation. If $input is the original string, use:
$intermediate = str_replace(" ", "+", $input);
$result = str_replace("'", "%27", $intermediate);
There is a built in function to do the same you are trying to do: urlencode
$currenttrack = $artist = "X' xx WWW' w";
$url = "http://last.fm/artist/" . $artist . "/_/" . $currenttrack;
echo urlencode($url); // http%3A%2F%2Flast.fm%2Fartist%2FX%27+xx+WWW%27+w%2F_%2FX%27+xx+WWW%27+w
The Simple HTML DOM library is used to extract the timestamp from a webpage. strtotime is then used to convert the extracted timestamp to a MySQL timestamp.
Problem: When strtotime() is usede on a valid timestamp, NULL is returned (See 2:). However when Simple HTML DOM is not used in the 2nd example, everything works properly.
What is happening, and how can this be fixed??
Output:
1:2013-03-03, 12:06PM
2:
3:1970-01-01 00:00:00
var_dump($time)
string(25) "2013-03-03, 12:06PM"
PHP
include_once(path('app') . 'libraries/simple_html_dom.php');
// Convert to HTML DOM object
$html = new simple_html_dom();
$html_raw = '<p class="postinginfo">Posted: <date>2013-03-03, 12:06PM EST</date></p>';
$html->load($html_raw);
// Extract timestamp
$time = $html->find('.postinginfo', 0);
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];
echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
2nd Example
PHP (Working, without Simple HTML DOM)
// Extract posting timestamp
$time = 'Posted: 2013-03-03, 12:06PM EST';
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];
echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
Output (Correct)
1:2013-03-03, 12:06PM
2:1362312360
3:2013-03-03 12:06:00
var_dump($time)
string(19) "2013-03-03, 12:06PM"
According to your var_dump(), the $time string you extracted from the HTML code is 25 characters long.
The string you see, "2013-03-03, 12:06PM", is only 19 characters long.
So, where are those 6 extra characters? Well, it's pretty obvious, really: the string you're trying to parse is really "<date>2013-03-03, 12:06PM". But when you print it into an HTML document, that <date> is parsed as an HTML tag by the browser.
To see it, use the "View Source" function in your browser. Or, much better yet, use htmlspecialchars() when printing any variables that are not supposed to contain HTML code.
The XML output from this loop was failing to validate but the validator was giving me different errors each time. Each time it had to do with the opening < of an element closure being missing. A different one each time...
Every time I refresh and re-validate the output there is at least one of these and it has never yet been in the same member record.
Initially I was adding tags everywhere which is why you will see many of them wrapping things where they should not be needed.
The XML is built by this loop:
if ($members) {
$xml = '<api><response status="ok"><users>';
foreach ($members as $m) {
$join_date = date("Y-m-d H:i:s", $m->join_date);
list($md) = $mdObj->retrieve("member_id = '$m->member_id'");
$join_date = ($m->join_date > 0) ? date("Y-m-d H:i:s", $m->join_date) : '0000-00-00 00:00:00';
$address = preg_replace('/\R/', '', $md->m_field_id_3);
$xml .= "<user id=\"$m->member_id\"><admin>0</admin><name><![CDATA[$m->username]]></name><company>$md->m_field_id_9</company><company_id>$md->m_field_id_28</company_id><address><![CDATA[$address]]></address><city>$md->m_field_id_5</city><region>$md->m_field_id_6</region><postal_code>$md->m_field_id_7</postal_code><email><![CDATA[$m->email]]></email><phone>$md->m_field_id_10</phone><first>$md->m_field_id_1</first><last>$md->m_field_id_1 $md->m_field_id_2</last><url></url><description><![CDATA[]]></description><status>active</status><date>$join_date</date><modified>0000-00-00 00:00:00</modified></user>";
}
$xml .= '</users></response></api>';
return $xml;
}
Has anyone seen this before? Have any advice?
Here's a little PHP info:
PHP Version 5.2.17
Linux foo.foo.com 2.6.18-274.17.1.el5 #1 SMP Wed Jan 4 22:45:44 EST 2012 x86_64
Build Date Feb 8 2012 14:19:50
I suspect the database entries you're including into you XML might contain unescaped characters which have special meaning, e.g. &, <, >, " and ' which need to be encoded.
I would also break up that long string into
$xml .= "<user id=\"" . $m->member_id . "\"><admin>0</admin><name><![CDATA[";
$xml .= $m->username . "]]></name><company>" . $md->m_field_id_9 . "</company>";
$xml .= "<company_id>" . $md->m_field_id_28 . "</company_id><address><![CDATA[";
$xml .= $address . "]]></address><city>" . $md->m_field_id_5 . "</city><region>";
$xml .= $md->m_field_id_6 . "</region><postal_code>" . $md->m_field_id_7;
$xml .= "</postal_code><email><![CDATA[" . $m->email . "]]></email><phone>";
$xml .= $md->m_field_id_10 . "</phone><first>" . $md->m_field_id_1 . "</first>";
$xml .= "<last>" . $md->m_field_id_1 . $md->m_field_id_2 . "</last><url></url>";
$xml .= "<description><![CDATA[]]></description><status>active</status><date>";
$xml .= $join_date . "</date><modified>0000-00-00 00:00:00</modified></user>";
and then use str_replace() to specifically encode the above-mentioned characters.
What could be happening is that you data contains invisible whitespace, most notably DEL characters .. I suppose that would cause this precise behaviour.
To check, loop over each character in the string and print the character code to check if a string contains any hidden whitespace.
This appears to be a bug in Chrome's view source routine on a large XML file. XML obtained from the same source via IE and FireFox was valid across repeated tests.
Additionally Chrome's normal view did not display these aberrations and did not report errors in the XML in normal view.
Using Zend _gdata. For some reason, recently the $when string is no longer utf-8. I need to convert it to utf-8. All the other fields are working fine.
foreach ($feed as $event) { //iterating through all events
$contentText = stripslashes($event->content->text); //striping any escape character
$contentText = preg_replace('/\<br \/\>[\n\t\s]{1,}\<br \/\>/','<br />',stripslashes($event->content->text)); //replacing multiple breaks with a single break
$contentText = explode('<br />',$contentText); //splitting data by break tag
$eventData = filterEventDetails($contentText);
$when = $eventData['when'];
$where = $eventData['where'];
$duration = $eventData['duration'];
$title = stripslashes($event->title);
echo '<li class="pastShows">' . $when . " - " . $title . ", " . $where . '</li>';
}
How do I make $when utf-8?
Thanks!
Depending on what encoding that string is using, you should be able to encode it to UTF-8 using one of the following functions :
utf8_encode()
iconv()
For example :
$when = utf8_encode($eventData['when']);
Or :
$when = iconv('ISO-8859-1', 'UTF-8', $eventData['when']);
If the string is in Latin1 you can just do what Pascal suggests.
Otherwise you need to find out which encoding it is.
Therefor check your php.ini settings or you can try to detect it by mb_detect_encoding (be aware it's not fail prove)