Cleaner way to use array_search? - php

I have the following string after it has been parsed by regex:
FOO_BAR_FOOBA_*_R:FOO_*
I am supposed to get rid of "FOOBA" so I thought that just by doing, should have been enough.
$size[0] = array('qwe', 'rty', 'uiop', 'asdf');
$size[1] = array('ASD' , 'FGHJ' , 'ZXCVB');
$size[2] = array('lol' , 'cat' , 'woof');
$pieces = explode("_",$noGen);
foreach ($size[0] as $value){
$search = array_search($size[0], $pieces);
unset($pieces[$search]);
}
However, I know that the code above pretty much would have worked out until I found out that not all the time the third "piece" exists and that the second piece sometimes can contain same values as the third one.
Is there a way to do this without having to use a foreach per size. If it is not clear I could add more information or just ping me.
Thank you in advance!

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;

Parsing pagination in php

I'm working with indexing some news sites. A kind of news clipping.
I'm an amateur and curious. I'm not a programmer so the question may seem silly to anyone in the business. But if anyone can help, thank you.
The paging of the sites I was doing parsing was practically the same and I used this scheme:
$url = $ url. '/page/'. $s;
$next_url = $s + 1;
$prev_url = $s - 1;
if ($prev_url <= 0) {
$prev_url = 1;
}
The format was basically this:
http://example.com/politics/page/2
But yesterday I came across something different and I do not know how to page. I get this link format through preg_match_all:
http://www.example.com/browse-Politics-National-texts-1-date.html
This is the paging part:
-1-
This part is variable:
Political-National-texts
Any guidance?
If what you are asking for is parsing the url for the pagination and variable parts, you can use preg_match with the following regexp:
if (preg_match('/^http:\/\/www.example.com\/browse-([-a-zA-Z]+)-(\d+)-date\.html$/', $url, $matches)) {
var_export($matches);
}
Then you will get the result:
array (
0 => 'http://www.example.com/browse-Politics-National-texts-1-date.html',
1 => 'Politics-National-texts',
2 => '1',
)
The keys in $matches will be:
0: The entire match
1: The first matched group (the variable)
2: The second matched group (the pagination)
<?php
$url = 'http://www.example.com/browse-Politics-National-texts-1-date.html'
$url_basename = basename($url); // extract `browse-Politics-National-texts-1-date.html`
$url_exploded = explode('-',$url_basename); // make an array delimited by `-`
array_pop($url_exploded);
$url_page_number = array_pop($url_exploded); // get the 2nd element from back
?>
Result:
$url_page_number = 1
PS. Could make it shorter, but it's for educational purposes :-)

How to replace some character in mysql column by 'x'?

I have emails like this
uuak6G6GgD#gmail.com
d3lferM#gmail.com
Efqc9#gmail.com
How to convert and select like this.
uuakxxxxxx#xxxxx.xxx
d3lfxxx#xxxxx.xxx
Efqcx#xxxxx.xxx
Yes I am able to get this value but, I am not good enough in mysql function so please help me to make it simple and short if possible. And also suggest what other solution would be to make it.
I have created my query as
SELECT CONCAT(LEFT(email,
4),SUBSTR(REGEXP_REPLACE(email,'[a-z0-9]',"x"),5)),email
FROM `users`
I am using PHP as server side, so If we could involve php to make good enough, please also suggest.
Your help and suggestions are heartily appreciable.
If the point is obfuscation of email addresses, then you should not replace the characters by x, thus giving indication on the length of the email address. Just take the first 4 characters (left() function) and add a fixed ending of "xxxxxxxx#xxxx.xxx". You also need to decide how to handle email addresses where the user part is shorter than 4 characters.
If you want to achieve this using PHP you can simply use the following regex along with preg_replace function like as
(^(\w){1,4}|#|\.)(*SKIP)(*F)|(.)
Example :
echo preg_replace("/(^(\w){1,4}|#|\.)(*SKIP)(*F)|(.)/","$1x","uuak6G6GgD#gmail.com");
I would suggest something like :
$email = substr($email, 0, 4) . preg_replace('/\w/', 'x', substr($email, 4));
Doing it in the MySQL query is usually not reasonnable imo. MySQL isn't very good at that kind of data transformation.
$emails = [
'uuak6G6GgD#gmail.com',
'd3lferMGo7#gmail.com',
'Efqc90dUGI#gmail.com',
'I#gmail.com',
];
$modded = [];
foreach ($emails as $item) {
$name = explode('#', $item);
$name = str_pad(substr($name[0], 0, 4), 4, "x");
$modded[] = $name . 'xxxx#xxx.xxx';
}
var_dump($modded);
involve php, select record and than
Break every email address in two parts
$email = 'I#gmail.com';
$email = explode('#', $email);
$firstPart = $email[0];
$lastPart = $email[1];
$first4 = substr($firstPart, 0, 4);
$mid = preg_replace('/[^.#\-]/', 'x', substr($firstPart, 4));
$last = preg_replace('/[^.#\-]/', 'x', $lastPart);
$converted = $first4.$mid.'#'.$last;

PHP how can i hide part of url

I'm working on a project and need to hide part of a url on the output result of my php file, how can i do that?
the piece of code
if (!$foundPlaylist){
$playList=array(
['publishedAt'],
'thumbId' => $entry[$i]['snippet']['thumbnails']['medium']['url'],
'videosCount' => $videoCount,
'videos' => getVideos($entry[$i]['snippet']['resourceId']['videoId'])
);
array_push($MainFeed,$playList);
}
The result
{ "feed":[{"thumbId":"https://i.ytimg.com/vi/SEchOz24pd8/mqdefault.jpg","videosCount":20,"videoid":"SEchOz24pd8",}],"0":
I need to hide https://i.ytimg.com/vi/ and /mqdefault.jpg from thumbId.
Just use
substr($entry[$i]['snippet']['thumbnails']['medium']['url'], 23, 11);
to select only the part of the URL between position 23 and (23+11) = 34
This, of course, only works if you know the string length is going to be exactly the same for all users. If you know the string length will differ, Anthony's answer might help you out.
I find this most readable:
$path = parse_url(
$entry[$i]['snippet']['thumbnails']['medium']['url'],
PHP_URL_PATH
);
list($user, $code, $image) = explode('/', $path);
echo $code;

Large text replace array

I'm looking for some help when replacing text from when i'm importing an XML file. I want to text-replace some values when importing, so it matches my categories, filter values etc. on my website.
I'm using this function. i wrote it myself with copy-pasting from internet (i'm not a coder) but now i need some help/advice.
<?php
// Text replace test function
function my_text_replace($x) {
for ($y = 0; $y < 2; $y = $y+1) {
$phrase = $x;
$old = array("Draaideurkast", "fout1 MRC", "Draaideurkast MRC", "Draaideurkast MRC");
$new = array("fout1", "fout2", "goed", "fout3");
$x = str_ireplace($old, $new, $phrase);
$y = $y+1;
return $x;
}
}
?>
Code Fix:
What happens is that i do not want a partial match replace, but only the complete value of $x. in the example the output should be 'goed'. it only should replace once when found. (but that is fixed with the for loop i think). the output should be case insensitive.
Advice question:
is this a correct way of replace (large amounts) of texts during an import? you guys know other best practises or plugins (wordpress) or tools..
Thanks for any response!
Harm

Categories