I have a post.I'm making an array explode this post.I have two rules when explode the post.
First Rule: The target must be between a tag.
Second Rule: The words must be explode down according to the space between them.
There is no problem up to this point.You can see it from the code below.
$demo = '<p>This is a red image.Red images do not look good under natural light.This is due to the
saturation rates.
<info><a href="http://localhost/img/red_image.jpg"><img class="danger_image"
src="http://localhost/img/red_image.jpg" alt="red_image_info"/></a>
</info></p>';
$demo = str_replace("<info", "*<info", $demo);
$demo = str_replace("</info>", "</info>*", $demo);
$new_array = array();
foreach (explode('*', $demo) as $demo_loop) {
$new_array[] = explode(' ', $demo_loop);
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
While doing the joining process, I place comment lines between the words.The problem arises after implode the array.
implode(" <!--test--> ",$new_array[$i][$is]);
I complete this implode in a loop.The picture is not displayed while the text is displayed normally.
The output is like this:
This is a red image.Red images do not look good under natural light.This is due to the saturation
rates.
href="http://localhost/img/red_image.jpg"> class="danger_image"
src="http://localhost/img/red_image.jpg" alt="red_image_info"/>
As a result of my experiments, I found that the comments lines caused this.If I can delete these comment lines inside the target tag, I will succeed what I want.
But I got stuck at this point
have you tried the "in_array" function? Maybe you can distinguish this way.
if (in_array("<info>", $new_array)) {
echo "Found! -> Delete Comment Lines";
}else {
echo "Not Found!";
}
You can then delete the comment lines for arrays that do not meet the condition.Remember that this is just an idea.
Related
I've been trying to figure out how to split the array and add different titles for each of the separate titles on the page, for each of the different things that this displays. However the most I can manage to do is add a comma between the numbers and words.
I would like to add selling"1st variable price"second variable" etc however I don't quite know how to do anything other than to turn this very confusing looking bunch of letters:
user name and notes 01001000013972583957ecCCany amount-w378- v west
into anything other than this:
0,100,10000,1397258395,7ec,CC,any amount-w378- v west
Also, this is what it looks like in its JSON form:
{"selling":"0","quantity":"100","price":"10000","date":"1397258395","rs_name":"7ec","contact":"CC","notes":"any amount-w378- v west"}
I just want all the information that is in there to displayed like that however I'm not quite sure how to add the titles that is in the JSON data. I also don't have access to the external site to change anything.
A little background: what I am trying to achieve is a price look-up for a game on my website from an external site. I tried to use an iframe but it was terrible; I would rather just manually display it rather than showing their site from mine - their style and my style clash terribly.
$json = file_get_contents('http://forums.zybez.net/runescape-2007-prices/api/rune+axe');
$obj = json_decode($json,true);
$blah1 = implode( $obj[0]["offers"][1]);
print_r($blah1);
If you know where it is, you should be able to just grab it and show it out?
You can use a failsafe to check if it is present with is_array() and isset() functions - see php.net docs on them.
Your print_r should give you good valid info -- try to wrap it around <pre></pre> tags before for better readability or view the source - it will be easier!
<pre><?php print_r($obj) ?></pre>
This should be your starting point, and from here you will either take the first one of your items or loop through all with
foreach ($obj as $o) { //should be $objects, not $obj
//do whatever with $o, like echo $o['price']
}
Each offers row is a table with each field separated by row:
$item = json_decode(file_get_contents('http://forums.zybez.net/runescape-2007-prices/api/rune+axe'));
while ($offer = array_shift($item[0]->offers)) {
echo "<table>" . PHP_EOL;
foreach ($offer as $field => $value) {
echo "<tr><th>$field</th><td>$value</td></tr>" . PHP_EOL;
}
echo "</table>" . PHP_EOL;
}
http://codepad.org/C3PQJHqL
Tables in HTML:
http://jsfiddle.net/G5QqZ/
I'm not sure what the terminology is, but basically I have a site that uses the "tag-it" system, currently you can click on the tags and it takes the user to
topics.php?tags=example
My question is what sort of scripting or coding would be required to be able to add additional links?
topics.php?tags=example&tags=example2
or
topics.php?tags=example+example2
Here is the code in how my site is linked to tags.
header("Location: topics.php?tags={$t}");
or
<?php echo strtolower($fetch_name->tags);?>
Thanks for any hints or tips.
You cannot really pass tags two times as a GET parameter although you can pass it as an array
topics.php?tags[]=example&tags[]=example2
Assuming this is what you want try
$string = "topics.php?";
foreach($tags as $t)
{
$string .= "tag[]=$t&";
}
$string = substr($string, 0, -1);
We iterate through the array concatenating value to our $string. The last line removes an extra & symbol that will appear after the last iteration
There is also another option that looks a bit more dirty but might be better depending on your needs
$string = "topics.php?tag[]=" . implode($tags, "&tag[]=");
Note Just make sure the tags array is not empty
topics.php?tags=example&tags=example2
will break in the back end;
you have to assign the data to one variable:
topics.php?tags=example+example2
looks good you can access it in the back end explode it by the + sign:
//toplics.php
<?php
...
$tags = urlencode($_GET['tags']);
$tags_arr = explode('+', $tags); // array of all tags
$current_tags = ""; //make this accessible in the view;
if($tags){
$current_tags = $tags ."+";
}
//show your data
?>
Edit:
you can create the fron-end tags:
<a href="topics.php?tags=<?php echo $current_tags ;?>horror">
horror
</a>
I'm pretty new to PHP so please bear with me for this one.
I have an array with emoticons, and I want to replace the emoticon text with the correct image, all within a for loop. So I'm trying to take my text variable and do a str_replace, but I'm not sure exactly how to display the text after the emoticons have been changed.
Here is my code:
$content = ":D Here is a sample sentence for this example :)";
$emotes = array(
[":)","<img class='emoticon' src='smile.png'>"],
[":D","<img class='emoticon' src='grin.png'>"],
);
for($i=0;$i<count($emotes);$i++) {
$contentWithEmotes = str_replace($emotes[$i][0], $emotes[$i][1], $content);
}
print $contentWithEmotes;
The problem this this is that it only displays the last image from the array, when I want it to display both of them.
How should I go about displaying the content with the correct image?
Thanks in advance for any help.
Restructure your array like this:
$emotes = [
":)"=>"<img class='emoticon' src='smile.png' />",
":D"=>"<img class='emoticon' src=grin.png' />"
];
Then use strtr:
$contentWithEmotes = strtr($content,$emotes);
Each time through the loop you need to process the result of the previous time, not the original content.
$contentWithEmotes = $content;
foreach ($emotes as $emote) {
$contentWithEmotes = str_replace($emote[0], $emote[1], $contentWithEmotes);
}
However, the strtr() solution is better.
I am working on a WordPress site where the client has uploaded thousands of photos to Flickr and now want me to move them all back into WordPress and associate them with there proper posts.
Even though there are thousands of images, there is really only about 50 unique images, all the other versions are the same image but uploaded to a different location on Flickr or a slightly different size or name.
In helping me track down all the unique images, based on a list like below, that part I have highlighted, I need to pull every record into a PHP array, the catch is the part I have highlighted, is what I want to make sure is UNIQUE among all records in the array.
Any help in taking an existing PHP ARRAy that has every record and making the array only show unique values based on that part of the Value string?
Is this a Regular Expressions use case?
If it used Regex or similar I think a pattern it could look for is /4485116555_ / followed by 10 digits and then followed up with a _
Appreciate any help in getting me 1 step closer to my goal, this is just 1 piece of the big puzzle.
http://farm5.static.flickr.com/4042/4485116555_19cc0eaa85.jpg
http://farm3.static.flickr.com/2703/4485767454_77476dbdd0.jpg
http://farm5.static.flickr.com/4008/4485116637_ff085b0ab2.jpg
http://farm5.static.flickr.com/4002/4485766896_af83d349c4.jpg
http://farm5.static.flickr.com/4037/4485766950_50d5739344.jpg
http://farm3.static.flickr.com/2785/4485116905_1fa0e2ea6c.jpg
http://farm5.static.flickr.com/4052/4704387613_77542dac2e.jpg
http://farm3.static.flickr.com/2734/4485767622_7b04c3bd3e.jpg
http://farm5.static.flickr.com/4037/4485767292_1a37fe6c57.jpg
http://farm5.static.flickr.com/4038/4485116955_f9c47672c3.jpg
http://farm5.static.flickr.com/4051/4485115681_6d7419a00b.jpg
http://farm3.static.flickr.com/2753/4485116095_30161a56bb.jpg
http://farm5.static.flickr.com/4123/4831194968_3977dff9dc.jpg
http://farm5.static.flickr.com/4054/4538941056_cda5a8242d.jpg
http://farm3.static.flickr.com/2091/4515081466_43cd1624ce.jpg
http://farm3.static.flickr.com/2684/4485766664_3bb9dd9c80_m.jpg
http://farm5.static.flickr.com/4010/4485115557_a38aac0e1f.jpg
http://farm5.static.flickr.com/4055/4485115633_19e6e92276.jpg
http://farm5.static.flickr.com/4045/4485766710_08691e99ed_m.jpg
http://farm5.static.flickr.com/4024/4485115521_9ab2a33d53_m.jpg
http://farm5.static.flickr.com/4048/4505577820_81ce080f2a_t.jpg
http://farm6.static.flickr.com/5294/5389182894_920a54ce97_m.jpg
http://farm5.static.flickr.com/4152/5073487038_5bdb9e3cbc_t.jpg
http://farm5.static.flickr.com/4024/4485115401_67a8957509_m.jpg
http://farm5.static.flickr.com/4062/4485766842_2209843592_m.jpg
$ids = array(); // Where we will keep our unique list of IDs
$lines = array(/* your list of URLs here */);
foreach ($lines as $line) {
preg_match(
'|^http://[A-Za-z0-9\\.]+/[0-9]+/([0-9]+)_[a-f0-9]+.*\\.jpg$|',
'http://farm5.static.flickr.com/4054/4538941056_cda5a8242d.jpg',
$matches
);
echo $matches[1]; // 4538941056
$ids[] = $matches[1]; // Push that into the IDs array
}
$ids = array_unique($ids);
print_r($ids);
Use this code to get your ID Portion
$url = 'Your image url';
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$end = end($pathFragments);
$id = substr($end,0,9);
And then run array_unique() to get the unique values.
I'm putting together a project for class that has to do with aggregating tweets in succession to create a linear, crowdsourced story. I've currently got a semi-working program, with the issue that the program displays the most recent tweet first - what I need is to sort these tweets to have them displayed by timestamp, oldest to newest. Here's the code:
<?php
$url = 'http://search.twitter.com/search.json?q=%23tweetstoryproj&lang=en&rpp=100';
$jsontwitter = file_get_contents($url);
$twitter = json_decode($jsontwitter, true);
$twittertext = $twitter["results"];
foreach($twittertext as $text){
$text = str_replace("#tweetstoryproj", "", $text);
echo $text['text'].'';
}
?>
Thanks a lot for your help, and if you want to contribute to the story, tweet with the hashtag: #tweetstoryproj
Simple! (He says) Just use array_reverse
Add this line in before the foreach
$twittertext = array_reverse($twittertext);
Enjoy!
Alternative:
If the array becomes huge it may take longer to flip it then traverse it, so you could use a "for" loop and go backwards thusly.
for($i=$count($twittertext);$i>-1;$i--) //It's late that may miss the last or the first entry, have a fiddle!
{
// echo here
}