php preg_match grab out - php

<script type="text/javascript"><!--
Vertical1_437 = "false-2010";
ShowAdHereBanner1437 =" true";
RepeatAll1437 = "true";
NoFollowAll1437 ="true";
//-->
</script>
I'm trying to get the 2010 part out of the false-2010.
i want it to echo 2010 only..
Thanks for the help.
this was what i started with and got stuck
<?php
$get2010 = preg_match('/\<!--(.*?)-->/', $get2010, $m);
echo $m[1];
?>
and oh.. the 2010 is a randomly generated number... it changes.

preg_match('!Vertical1_437\s*=\s*"\D+(\d+)"!', $get2010, $m);
echo $m[1];
This assumes it's always the RHS of Vertical1_437. I am also assuming you have that block of HTML code as a string in $get2010.

It may be helpful if you could be more specific about what data is dynamic and what is static. If Vertical1_437 and false are static and the 2010 part is the only dynamic part, then this should work:
preg_match{'#(.*Vertical1_437.*false-)(\d{1,4})(.*)#', $get2010, $m);
echo $m[2];
Mine assumes that "2010" is a digit that is 1-4 digits, you can adjust that in the {1,4} part.
This should do it for the given example. If some of that other text is dynamically generated, then There will probably be something to change.

Related

how to do echo from a string, only from values that are between a specific stretch[href tag] of the string?

[PHP]I have a variable for storing strings (a BIIGGG page source code as string), I want to echo only interesting strings (that I need to extract to use in a project, dozens of them), and they are inside the quotation marks of the tag
but I just want to capture the values that start with the letter: N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
how to do that to define that the echo will delete all the texts of the variable, showing only:
note that there are other hrefs tags with values that start with other letters, such as the letter 'P' : href="/profiles... (This does not interest me.)
$string = '</div><span class="news-hd-mark">HD</span></div><p>exclusive_news_sunday_</p><p class="metadata"><span class="bg">Czech AV<span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
I imagine something like this:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
expected output: /news7044449/exclusive_news_sunday_
NOTE: it is not essential to be through a variable, it can be from a .txt file the place where the extracts will be extracted, and not necessarily a variable.
thanks.
I believe this will help her.
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
Step by Step Regex:
Regex Demo
Regex Debugger
To get just the links out of the $results array from Valdeir's answer:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}

Regex to select url except when = is directly infront of it

I'm trying to use a regex to find and replace all URLs in a forum system. This works but it also selects anything that is within bbcode. This shouldn't be happening.
My code is as follows:
<?php
function make_links_clickable($text){
return preg_replace('!(([^=](f|ht)tp(s)?://)[-a-zA-Zа-яА-Я()0-9#:%_+.~#?&;//=]+)!i', '$1', $text);
}
//$text = "https://www.mcgamerzone.com<br>http://www.mcgamerzone.com/help/support<br>Just text<br>http://www.google.com/<br><b>More text</b>";
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Unparsed text:</b><br>";
echo $text;
echo "<br><br>";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
?>
All urls that occur in bb-code are following up on a = character, meaning that I don't want anything that starts with = to be selected.
I basically have that working but this results in selecting 1 extra character in in front of the string that should be selected.
I'm not very familiar with regex. The final output of my code is this:
<b>Unparsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa<br>
<br>
<b>Parsed text:</b><br>
#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa
You can match and skip [url=...] like this:
\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)
See regex demo
That way, you will only match the URLs outside the [url=...] tag.
IDEONE demo:
function make_links_clickable($text){
return preg_replace('~\[url=[^\]]*](*SKIP)(?!)|(((f|ht)tps?://)[-a-zA-Zа-яёЁА-Я()0-9#:%_+.\~#?&;/=]+)~iu', '$1', $text);
}
$text = "#Theareak We know this and [b][url=https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks]here[/url] [/b]is an explanation, we are trying to fix this asap! https://www.mcgamerzone.com/news/67/False-positive-proxy-bans-and-bot-attacks aaa";
echo "<b>Parsed text:</b><br>";
echo make_links_clickable($text);
You can use a negative lookbehind (?<!=) instead of your negated class. It asserts that what is going to be matched isn't preceded by something.
Example

Finding php string within string and save as variable

If I had a string of text e.g:
<p>Hello world here is the latest news ##news?123## or click here to read more</p>
I want to look through the string and find anything starting with ##news? Then I want to save that and the id 123 and trailing hashes as a variable, so the final output would be:
$myvar == "##news?123##"
How can I use PHP to read that input string and save that specific part as a variable?
preg_match is your friend for this sort of problem.
$str='<p>Hello world here is the latest news ##news?123## or click here to read more';
$pttn='#(##news\?(\d+)##)#';
preg_match( $pttn, $str, $matches );
$myvar=$matches[0];
$id=$matches[2];
echo $myvar.' '.$id;
Assuming you're not trying to parse HTML but as you say, just find any occurrence starting with "##news?" then you can use regex happily here:
$string = '<p>Hello world here is the latest news ##news?123## or click here to read more';
$matches = array();
preg_match_all("/(##news\?.*?)\s/",$string,$matches);
print_r($matches);

regular expression for href from html page in php ?

i want get all/farsi_persian/*/subtitle-*.aspx from one html page
i try some regular expression on PHP but not find
can help me ?>
<span class="r0" title="Rating 0 out of 10">Farsi/Persian</span> Arrow - Third Season subtitle<br><small> Arrow.S03E03.HDTV.480p.x264-LOL </small>
Try
/farsi_persian/[^/]+/subtitle-[^.]+.aspx
try using preg_match_all(), this might work::
preg_match_all('/\/farsi_persian\/[\w-]+\/subtitle-[\d]+.aspx/', $str, $matches);
assuming there's always numbers after subtitle-
$myPattern = "/farsi_persian/[^/]+/subtitle-[^.]+.aspx";
preg_match_all($myPattern,$myText,$match);
var_dump($match);
it show null;
its worked
$myPattern = "/farsi_persian\/(.*.)\/subtitle-\d*.aspx/";

php regex to get middle of string

I parse an html page into a plain text in order to find and get a numeric value.
In the whole html mess, I need to find a string like this one:
C) Debiti33.197.431,90I - Di finanziamento
I need the number 33.197.431,90 (where this number is going to change on every html parsing request.
Is there any regex to achieve this? For example:
STARTS WITH 'C) Debiti' ENDS WITH 'I - Di finanziamento' GETS the middle string that can be whatever.
Whenever I try, I get empty results...don't know that much about regex.
Can you please help me?
Thank you very much.
You could try the below regex,
^C\) Debiti\K.*?(?=I - Di finanziamento$)
DEMO
PHP code would be,
<?php
$mystring = "C) Debiti33.197.431,90I - Di finanziamento";
$regex = '~^C\) Debiti\K.*?(?=I - Di finanziamento$)~';
if (preg_match($regex, $mystring, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> 33.197.431,90
This should work. Read section Want to Be Lazy? Think Twice.
(?<=\bC\) Debiti)[\d.,]+(?=I - Di finanziamento\b)
Here is demo
sample code:
$re = "/(?<=\\bC\\) Debiti)[\\d.,]+(?=I - Di finanziamento\\b)/i";
$str = "C) Debiti33.197.431,90I - Di finanziamento";
preg_match($re, $str, $matches);

Categories