Preg Replace text based on string - php

i am trying to figure out why this has no result.
I am fetching data from wp database
$global_notice2 = get_post_meta($post->ID,'_global_notice', true);
This contains an a href link i wish to manipulate using preg replace before displaying it for the user such as
preg_replace('/<a(.*?)href="(.*?)"(.*?)>/', '', $global_notice2 );
Now we display the data
$notice2 = "<p>$alternative_content$global_notice2</p>";
The data is unmodified, what am i doing wrong?

preg_replace don't modify the argument, you need to catch the return like this :
$global_notice2 = preg_replace('/<a(.*?)href="(.*?)"(.*?)>/', '', $global_notice2);
See preg_replace documentation

Related

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.

Remove certain part of Posted value with php

Hello everyone im trying to retrieve data from a form with a POST request.
This data is posted into another website.
On the website where the data is created i have a text field called website. The data filled in this field goes to the other website where the data is collected. Now i want to exclude the 'www' part. for example if the user enters www.hello.nl i want to receive hello.nl only.
What i tried:
function website () {
$str = $_POST['billing_myfield12'];
echo chop($str,"www");
}
// end remove www
// prepare the sales payload
$sales_payload = array(
'organization_id' => $organization_id,
'contact_id' => $contact_id,
'status' => 'Open',
'subject' => $_product->post_title." ".website(), <----- here i call it
This is not working. Is there a way to do this?
You can use trim() or specifically ltrim() to trim way the www. on the left side. Please don't forget the . after www.
echo ltrim($str, "www.");
Sample Code
echo ltrim("www.hello.nl", "www."); // hello.nl
Demo: http://ideone.com/bqMY7X
Looks like there are side effects with the above code. Let's go with the traditional str_replace method:
echo str_replace("www.", "", $str);
Also, we are sure that it should replace only from the first characters. So, we need to use a preg_replace instead, making it replace from the start.
echo preg_replace("/^www\./g", "", $str);
Verified the above code with: https://regex101.com/r/dv8N6d/1

PHP For Loop str_replace emoticons

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.

Regex in PHP to extract data from website

I am new to php. As a part of my course homework assignment , I am required to extract data from a website and using that data render a table.
P.S. : Using regex is not a good option but we are not allowed to use any library like DOM, jQuery etc.
Char set is UTF-8.
$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);
$patternform = '/<form(.*)<\/form>/sm';
preg_match_all($patternform ,$html,$matches);
Here regex works fine but when I apply the same regex for table tag, it return me empty array. Is there something to do with whitespaces in $html ?
What is wrong here?
The following code produces a good result:
$searchURL = "http://www.allmusic.com/search/artists/the+beatles";
$html = file_get_contents($searchURL);
$patternform = '/(<table.*<\/table>)/sm';
preg_match_all($patternform ,$html,$matches);
echo $matches[0][0];
Result:

How can I insert a string into another string?

I want to insert a string into another string.
I have youtube links:
http://www.youtube.com/9bZkp7q19f0
and I want to add /embed after the .com so that I can embed them on the fly.
How can I make them look like this?:
http://www.youtube.com/embed/9bZkp7q19f0
$url = str_replace("youtube.com/", "youtube.com/embed/", $url);
You can use "substr_replace" which means you are replacing text within a portion of a string.
Have a look to this, can get to know more about substr_replace,
http://php.net/manual/en/function.substr-replace.php
$link = "http://www.youtube.com/9bZkp7q19f0";
$link = str_replace("youtube.com/", "youtube.com/embed/", $link);
now
$link = "http://www.youtube.com/embed/9bZkp7q19f0";

Categories