PHP For Loop str_replace emoticons - php

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.

Related

How Can I Show the Picture After Merging the Array?

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.

Trying to grab value from html page but getting template back not the value - php

I am making a price crawler for a project but am running into a bit of an issue. I am using the below code to extract values from an html page:
$content = file_get_contents($_POST['url']);
$resultsArray = array();
$sqlresult = array();
$priceElement = explode( '<div>value I want to extract</div>' , $content );
Now when I use this to get certain elements I only get back
Finance: {{value * value2}}
I want to get the actual value that would be displayed on the screen e.g
Finance: 7.96
The other php methods I have tried are:
curl
file_get_html(using simple_html_dom library)
None of these work either :( Any ideas what I can do?
You just set the <div>value I want to extract</div> as a delimiter, which means PHP looks for it to separate your string to array whenever this occurs.
In the following code we use , character as a delimiter:
<?php
$string = "apple,banana,lemon";
$array = explode(',', $string);
echo $array[1];
?>
The output should be this:
banana
In your example you set the value you want to extract as a delimiter. That's why this happens to you. You'll need to set a delimiter between your string you want to obtain and other string you won't need at the moment.
For example:
<?php
$string = "iDontNeedThis-dontExtractNow-value I want to extract-dontNeedEither";
$priceElement = explode('-', $string);
echo "<div>".$priceElement[2]."</div>";
?>
The code should output this to your HTML page:
<div>value I want to extract</div>
And it will appear on your page like this:
value I want to extract
If you don't need to save the whole array in a variable, you can save the one index of it to variable instead:
$priceElement = explode('-', $string)[2];
echo $priceElement;
This will save only value I want to extract so you won't have to deal with arrays later on.

How can I get just the URL from this?

I get a lot of DMCA removal emails for my website, and I'm trying to automate the process of removing those tracks from my website.
The emails all come looking similar to this.
http://example.com/title-to-post.html title - to post
http://example.com/title-of-post.html title - of post
http://example.com/some-song-artist-some-song-name.html some song artist - some song name
But there's a lot of them, I only wanna return the URL portion of every part of this, example being below.
http://example.com/title-to-post.html
http://example.com/title-of-post.html
http://example.com/some-song-artist-some-song-name.html
EDIT: I am storing these files into a txt file, then calling them using standard code.
$urls = file_get_contents( "oururls.txt" );
$data = explode(",", $urls);
foreach ($data as $item) {
echo "$item";
echo '<br>';
}
Nothing really fancy going on, how ever it's returning the title also and I want just the urls.
If there's always a space after the URL you can explode the text by " " and get the first portion. Example:
$example = explode(" ", $url);
echo $example[0];

Replace certain content in txt file and save it using PHP?

So I receive variable replace it in certain area in txt file and save it back. I get page number and according to it I get exploded data. Anyway I'll post a code below to make it more clear:
$pgnm = $_GET['page']; //This is the page number as I've said.
$conts = file_get_contents("content.txt");
the content of content.txt looks like this:
text1|text2|text3
I display this content in certain pages. For example on first page: text1, on second text2, etc.
Now i'm working on a form where I successfully change these. I get as I've said page number and text:
$text = "new text"; //this is the content which I want to be replaced instead of text2.
I make the content.txt file look like this after its saved: text1|new text|text2
So lets go on:
$exp = explode("|", $conts); //this explodes data into slashes.
$rep = str_replace($exp[$pgnm], $text, $conts);
file_put_contents("content.txt", $rep); // Saving file
All these above-mentioned operations work perfectly, but here's my problem now. This only works if content.txt has certain content, if it's empty it enters my new text and that's all: 'new text' and that's all. Maybe I want to add second page content 'new text2' and after I finish entering it and save I want the file to be displayed like this: new text|new text2. If the content of content.txt looks like this: 'new text|' str_replace doesn't replace empty string. So that's my another problem too.
I tried everything, but couldn't manage to anything about this two problems. Thank you in advance for your help!
Why don't you use your $exp array for building the content. I mean $exp contains all the blocks as an array() one by one. So you just change, or add new values to the array (no str_replace() needed). Then rebuild using implode('|',$exp);.
As for your code;
$exp = explode("|", $conts); //this explodes data into slashes.
$exp[$pgnm] = $text;
file_put_contents("content.txt", implode('|',$exp)); // Saving file
Instead of str_replace use this code:
$pgnm = 1;
$text = 'new text';
$conts = 'text1||text3';
$exp = explode('|', $conts);
$exp[$pgnm] = $text;
$rep = implode('|', $exp);
var_dump($rep); // string(20) "text1|new text|text3"

Additional elements to URLS?

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>

Categories