PHP variable between 2 string positions - php

I have tried searching the forums but still unsure how to do this.
I am extracting a web link from a webpage, and the start and end are always the same but there is a variable i want to get.
E.g http://www.example.com/images/$VARIABLE/image.jpg
$position1 = http://www.example.com/images/
$position2 = image.jpg
I need to get the variable. On the webpage the $position 2 is listed more than once (image.jpg)
I have tried all sorts of things and nothing works so far.
Thank you
$begin = strpos($page, $position1) + strlen($position1);
$end = strpos($page, $position2);
$ImageName = substr($page, $begin, ($end - $begin));

I think the problem is that if image.jpg occurs before http://www.example.com/images/, then just trying to use your code won't work.
A slight fix would be to start the search for $position2 where you found the first part by adding a start point in the strpos() function...
$begin = strpos($page, $position1) + strlen($position1);
$end = strpos($page, $position2, $begin);
$ImageName = substr($page, $begin, ($end - $begin) - 1);
with
$page = "some test image.jpg some more text
blurb that is in the middle http://www.example.com/images/VARIABLE/image.jpg
some even more text image.jpg";
the code finds
VARIABLE
A regex as suggested by #user3783243 may also work, but you may also need a constraint of how long the parameter is likely to be.

Related

Simple PHP code for extracting data from the HTML source code

I know I can use xpath, but in this case it wouldn't work because of the complexity of the navigation of the site.
I can only use the source code.
I have browsed all over the place and couldn't find a simple php solution that would:
Open the HTML source code page (I already have an exact source code page URL).
Select and extract the text between two codes. Not between a div. But I know the start and end variables.
So, basically, I need to extract the text between
knownhtmlcodestart> Text to extract <knownhtmlcodeend
What I'm trying to achieve in the end is this:
Go to a source code URL.
Extract the text between two codes.
Store the data temporarily (define the time manually for how long) on my web server in a simple text file.
Define the waiting time and then repeat the whole process again.
The website that I'm going to extract data from is changing dynamically. So it would always store new data into the same file.
Then I would use that data (but that's a question for another time).
I would appreciate it if anyone could lead me to a simple solution.
Not asking to write a code, but maybe someone did anything similar and sharing the code here would be helpful.
Thanks
I (shamefully) found the following function useful to extract stuff from HTML. Regexes sometimes are too complex to extract large stuff, e.g. a whole <table>
/*
$start - string marking the start of the sequence you want to extract
$end - string marking the end of it..
$offset - starting position in case you need to find multiple occurrences
returns the string between `$start` and `$end`, and the indexes of start and end
*/
function strExt($str, $start, $end = null, $offset = 0)
{
$p1 = mb_strpos($str,$start,$offset);
if ($p1 === false) return false;
$p1 += mb_strlen($start);
$p2 = $end === null ? mb_strlen($str) : mb_strpos($str,$end, $p1+1);
return
[
'str' => mb_substr($str, $p1, $p2-$p1),
'start' => $p1,
'end' => $p2];
}
This would assume the opening and closing tag are on the same line (as in your example). If the tags can be on separate lines, it wouldn't be difficult to adapt this.
$html = file_get_contents('website.com');
$lines = explode("\n", $html);
foreach($lines as $word) {
$t1 = strpos($word, "knownhtmlcodestart");
$t2 = strpos($word, "knownhtmlcodeend");
if ($t1)
$c1 = $t1;
if ($t2)
$c2 = $t2;
if ($c1 && $c2){
$text = substring($word, $c1, $c2-$c1);
break;
}
}
echo $text;

PHP how to add variable string after strpos

I am getting all the data of a webpage then searching it for a certain string. I use strpos to find the location, then once found i would like to create a variable that stores all the information 64 characters past the strpos.
E.g:
$begin = strpos($page, 'content123') //Then once position is found on webpage, add another 64 characters beyond the 3 (of 123)
To clarify, i am getting file contents from a web page, searching the page data for a position (e.g 'content), then once i have found the position, I would like the program to grab everything after this marker by 64 characters (this is a variable)
EDIT:
$begin = strpos($page, 'https://i2.au.reastatic.net/800x600/') + strlen('https://i2.au.reastatic.net/800x600/');
$end = strpos($page, '/image.jpg');
$ImageName = substr($page, $begin, ($end - $begin));
$ImageContent = 'https://i2.au.reastatic.net/800x600/' . $ImageName . '/image.jpg';
P.S, not sure why people are down voting a legitimate question? I thought this was a place to help each other.
Thanks
it seems that your code is correct
$page = 'https://i2.au.reastatic.net/800x600/ImageName/image.jpg';
$begin = strpos($page, 'https://i2.au.reastatic.net/800x600/') + strlen('https://i2.au.reastatic.net/800x600/');
$end = strpos($page, '/image.jpg');
$ImageName = substr($page, $begin, ($end - $begin));
$ImageContent = 'https://i2.au.reastatic.net/800x600/' . $ImageName . '/image.jpg';
echo $ImageContent;

how to partially mask/hide email address using PHP

Im trying to achieve the following with PHP
sample#gmail.com => s*****#gmail.com
sa#yahoo.com => **#yahoo.com
sampleaddress#hotmail.com => samplead*****#hotmail.com
I want to hide last five characters in the portion that stays before '#'
I can write long code to do this by splitting and then replacing based on lengths, but Im sure there must be an easy way to do this using PHP functions, any help please?
UPDATE:
Im adding my code here, Im sure its not efficient, and thats the reason Im asking it here
$email = 'sampleuser#gmail.com';
$star_string = '';
$expl_set = explode('#',$email);
if(strlen ($expl_set[0]) > 5){$no_stars = 5; }else{$no_stars = strlen ($expl_set[0]); }
for($i=0;$i<$no_stars; $i++)
{
$star_string.='*';
}
$masked_email = substr($expl_set[0], 0, -5).$star_string.'#'.$expl_set[1];
You can wrap it into a function, making it easier to call multiple times.
Basically, split the address and the domain, replace $mask number of characters in the end of the string (default 5) with *, or the length of the address if it's shorter than the amount of masked characters.
function mask_email($email, $masks = 5) {
$array = explode("#", $email);
$string_length = strlen($array[0]);
if ($string_length < $masks)
$masks = $string_length;
$result = substr($array[0], 0, -$masks) . str_repeat('*', $masks);
return $result."#".$array[1];
}
The above would be used like this
echo mask_email("test#test.com")."\n";
echo mask_email("longeremail#test.com");
which would ouput this
****#test.com
longer*****#test.com
You can also specify the number you want filtered by using the second parameter, which is optional.
echo mask_email("longeremail#test.com", 2); // Output: longerema**#test.com
Live demo

How to get correctly content and avoid breaking html tags using strip_tags with substr?

In my page I have some post previews from RSS feeds. Every post preview shows about 300 characters. When a user clicks on expanding button, then the #post-preview is replaced with the #post. The #post shows the rest of the post.
Everything fine with this but the format of the #post is not good, not readable. So I thought of allowing <br><b><p> tags, it will make it ok to be read. Because I don't want the user to be distracted, I want the tags to be allowed after the 300 chars.
With the following method, it is possible to break some tags where the $start ends and $rest starts. This means no good readable output.
$start = strip_tags(substr($entry->description, 0, 300));
$rest = strip_tags(substr($entry->description, 300), '<b><p><br>');
$start . $rest;
My question is how can I keep $start and $rest the same (no tags) until the 300 char, and after that $rest will show the formatted post? Are there any other ways of doing this?
Here is an example of a RSS feed structure (from view page source).
<item><guid isPermaLink="false"></guid><pubDate></pubDate><atom:updated></atom:updated><category domain=""></category><title></title><description></description><link></link><author></author></item>
I am looking for a way that does not kill performance.
Something like:
$start = substr($entry->description, 0, 300);
if(($pos = stripos($start, "<")) !== false) {
$start = strip_tags(substr($start, 0, $pos));
$rest = substr($entry->description, $pos);
}
else {
$start = strip_tags($start);
$rest = substr($entry->description, 300);
}
Ok, it's just a concept. Gets first 300 chars and checks for broken tag. If broken cut before it and get $rest from this point. If not broken just strip and get rest. There is at least 1 problem:
you never now the length of the $start(after strip_tags could be nothing left), could use loop with length checking but eeee... efficiency
EDIT
Ok, get it:
$start = "";
$chars = 400;
while(strlen($start) < 300) {
$start = strip_tags(substr($rss, 0, $chars));
$chars += 50;
}
$pos = stripos($rss, substr($start, strlen($start) - 50));
$rest = substr($rss, $pos+50);
Ok, little nasty and there are some cases on which it fails(with repetable text probably:D), tested on Ideone

PHP Find Previous String Position

Is there a way that I can search a variable starting from a given position and find the start position of a string that is in the variable backwards from the given start position.
So for example if I initially do $getstart = strpos($contents, 'position', 0);
I then want to do $getprevpos = prevstrpos($contents, 'previous token', $getstart);
Obviously there is no such function as prevstrpos but I hope you get what I mean.
Example text area (terrible example I now):
Here is an example where I want to find the previous token once I have found the start position of a text string.
you can strrpos( substr($contents, 0, $getstart), 'previous token')
Is there something wrong with strrpos()? If 'offset' is negative: "Negative values will stop searching at the specified point prior to the end of the string."
you can try this. I think it should would for all cases but you should probly test it a bit. Might be a bug here and there but you get the idea. Reverse everything and do a strpos on the reversed string
prevstrpos( $contents, $token, $start )
{
$revToken = strrev($token);
$revStart = strlen($token) - $start;
$revContent = strrev($content);
$revFoundPos = strpos( $revContent, $revToken, $revStart );
if( $revFoundPos != -1 )
{
$foundPos = strlen($token) - $revFoundPos;
}
else
{
$foundPos = -1;
}
return $foundPos;
}

Categories