Preg_match somehow not finding part of string - php

I am having a problem with preg_match in that it is not returning anything. While according to: http://gskinner.com/RegExr/?=35ls9
It should be functioning properly.
This is my current code:
$string == <a class="twitter-timeline" href="https://twitter.com/...." data-widget-id="352777062139922223">....</a>...
its simply the embed code twitter throws out when creating a widget. Was also included in the example.
$string = get_field('twitter_feed'); //contains the string.
preg_match('/data-widget-id="([0-9]*)"/', $string, $match);
var_dump($match);
Its probably something really simple that i am missing. Hopefully somebody is able to help me with this problem.
edit: added the sample string.

Test it with the following string. I did, and it works fine:
$string = 'data-widget-id="352777062139922223"';
Make sure that get_field is returning a string in that form.

Related

php - Replace Template Code with HTML

I got a Button, that creates this template code
[5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)
The filename and file size vary und the user can change the ##LINKNAME##, as well.
This code goes to a database and when I get it back, I want to replace it to
##LINKNAME## <i>(96.51 kb)</i>
I think I need to use preg_replace() but I am not really good at regular expressions.
I stopped here:
<?php
$string = ' [5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)';
$regex = '[[a-zA-Z0-9]+.pdf](.*?)\s';
$replace = 'I DONT KNOW';
echo preg_replace($regex, $replace, $string);
?>
I know that this is a complete mess, but I'm not getting any results as long as I don't know the regex and the correct $replace.
Regex: ^\[([^\]]+)\](\w+)\s\(([^)]+)\)$
Replace with: \2<i>(\3)</i>
Demo

PHP: RegEx fails to match on HTML page retrieved using file_get_contents

$url = "http://steamcommunity.com/profiles/7656119XXXXXXXXX/";
$content = file_get_contents($url);
$regExd = '/<div class=\"profile_in_game_joingame\">\n\s+.?<a href=\"(steam.+?)\"[\s]class=\".+\"/i';
$a = preg_match($regExd, $content, $matches);
var_dump($a);
I am trying to retrieve a html from page using get_file_contents and then find specific URL from page using RegEx that I built here: https://regex101.com
I tried copying whole HTML source code from file_get_contents return and paste it into tester on that website and it worked perfectly but using my code - it fails to find any matches even though URL searched for is 100% on page.
HTML contents of return from get_file_contents:
<div class="profile_in_game_joingame">
<a href="steam://" class="btn_green_white_innerfade btn_small_thin">
<span>Join Game</span>
</a>
</div>
</div>
</div>
That's how it comes back, messed up with white spaces etc.. but if I test my regex rule on tester - it still works fine.
I know the URL is empty but that doesn't matter, its supposed to return steam:// in this case.
Okay nevermind, apparently I am an idiot, I went over documentation for regex and found issue.
I am not supposed to store preg_match in a variable, it generates array instead $matches.

Matching a pattern using web scraping

I want to get all the problems solved by a user on a website by using regular expression to get only the problem code through it. For example if PROBLEM is the HTML code I want to get only the PROBLEM from it. This is the php function which I wrote to accomplish this.
public function filter($s, $u) {
//$u -> username
//$s -> string containing html code
$reg= "/[^<a href=\"\/status\/(?:[A-Z]|[0-9]|\_|[a-z]|\.)*\"$u]/";
$solved = preg_split($reg, $s, -1, PREG_SPLIT_NO_EMPTY)
return $solved[0];
}
The regular expression doesn't seem to be correct and I am only getting /[^ when I print $reg. Also, I am not sure if preg_split() is the right function to return to do this. Please help.
The following works for me. Note that instead of searching for the PROBLEM text in the link as you were attempting, I search for the PROBLEM text that is being highlighted. The $u parameter no longer seems necessary.
public function filter($s) {
// $s set to 'PROBLEM';
$reg = '#(?P<problem>.+)#i';
preg_match($reg, $s, $matches);
return $matches['problem']
}
The outer brackets in your regular expression are out of place.

preg_match and remove multiple characters in string?

Hi I'm using php to program my site and I've been reading loads about preg_match and trying lots of examples. What I'm trying to do is something like below...
if(preg_match('These characters'), $myVariable, matches)){
Find and remove found characters from $myVariable;
}
I'm pretty sure this is obvious to php experts but it's had me stuck after hours of trying and reading.
Thanks in advance
You don't need to check for a match before doing a replace. It's like if you were to do:
str_replace("A","B","ZZZZZZZ");
It just won't replace anything. Same goes for preg_replace: If there is no match, it just does nothing.
It sounds like you should be using preg_replace. If you wanted to remove all y's and o's for example you would do this:
$string = 'hey you guys!';
$ans = preg_replace('/[yo]/','',$string);
print_r($ans); //outputs 'he u gus!'
Whatever characters you want to remove, just put them between the brackets [...]

getting number from external website

I need to get a number from this website: Current STC price which displays a market driven figure: STCs.
i tried this:
$html = file_get_contents('http://www.greenenergytrading.com.au/certificates/todays-pricing');
$html = strip_tags($html);
which leaves me with a long string. I then tried to remove anything before the figure I'm after, assuming that the text wont change:
$html = preg_replace('/.*Current STC price/', '', $html);
However, this doesnt work. it seems to work on online RexExp tester but not in production. also, is this a reasonable approach?
cheers
You can use preg_match with the $matches parameter provided to extract all ocurrences of from the website source and store them in an array. Then, just access the first element of the array.
Check out the documentation for preg_match here:
http://php.net/manual/en/function.preg-match.php
EDIT: Oh, I just saw your comment that you tried preg_match already. What regexp's have you tried? Have you tried something like "/$[0-9]{1}/" ?

Categories