Regex not finding all variables - php

I'm parsing some HTML, that I have generated in a form. This is a token system. I'm trying to get the information from the Regexp later on, but somehow, it's turning up only the first of the matches. I found a regexp on the Web, that did almost what I needed, except of being able to process multiple occurances.
I want to be able to replace the content found, with content that was generated from the found string.
So, here is my code:
$result = preg_replace_callback("/<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>\[\*.*\*\]\<\/[a]\>/i", array(get_class($this), 'embed_video'), $str);
public function embed_video($matches)
{
print_r($matches);
return $matches[1] . 'foo';
}
I really need only the attributes, since they containt all of the valuable information. The contents of the tag are used only to find the token. This is an example of what needs to happen:
<a type="TypeOfToken1" id="IdOfToken1">[*SomeTokenTitle1*]</a>
<a type="TypeOfToken2" id="IdOfToken2">[*SomeTokenTitle2*]</a>
After the preg_replace_callback() this should be returned:
type="TypeOfToken1" id="IdOfToken1" type="TypeOfToken2" id="IdOfToken2"
But, the callback function outputs the matches, but does not replace them with the return. So, the $result stays the same after the preg_replace_callback. What could be the problem?
An example with real data:
Input:
<p><a id="someToken1" rel="someToken1">[*someToken1*]</a> sdfsdf <a id="someToken2" rel="someToken2">[*someToken2*]</a></p>
returned $result:
id="someToken1" rel="someToken1"foo
Return from the print_r() if the callback function:
Array ( [0] => [*someToken1*] sdfsdf [*someToken2*] [1] => id="someToken1" rel="someToken1" [2] => rel="someToken1" [3] => rel [4] => ="someToken1" )
I think that it is not returning both of the strings it should.

For anyone else stumbling into a problem like this, try checking your regexp and it's modifiers.
Regarding the parsing of the document, I'm still doing it, just not HTML tags. I have instead gone with someting more textlike, that can be more easily parsed. In my case: [*TokeName::TokenDetails*].

Related

TextArea joins as one string to PHP how can I set the other strings in new line with help of delimiter

I've tried searching all over google and tried a similar problem and answers yet I still didn't get my problem done.
So basically, I'm making an HTML to write user input in a textbox which is the user would input:
Dog,
cat,
coronavirus,
Fever,
Cough,
Then into my PHP code, I capture it with:
$input = $_GET['contents'];
So I tried this one which I saw from googling and it doesn't have an explanation but logically its about array:
$input= explode(array(",", ""), $input)[0];
It works but the problem is that it only shows the first output which is:
Dog
Dog
Dog
Dog
Dog
But when I tried to remove the delimiter code, it shows one string only:
Dog,cat,coronavirus,Fever,Cough,
I thought about it something related to array but I'm not sure, is there any way to display all of them per line and with the help with delimiter?
UPDATE:
So I tried doing print_r($secs); and it shows like this:
Array ( [0] => Dog [1] => cat )
Array ( [0] => )
My accomplishment is to read all of the arrays one by one in another line instead of joining it as one strings. Sorry for bad english and im trying my best to explain.
you do not need the array() line.
Instead
if(strpos($_GET['contents'],',') !== false) { //check for commas first
$input = explode(',' , $_GET['contents']);
echo '<pre>';
print_r($input); //gives you the array
echo '</pre>';
}
to access each string individually, call the offset. For example, print_r($input[0]);
Also a friendly piece of advice. You seem like a beginner. You do not have to execute multiple commands in one line. Write your code step by step.

php sscanf doesnt parse string properly

I develop ADIF parser and parsing process comes to the point where I use sscanf() php function The strind that I parse is as following: "QSO_DATE:8:D>20070909" and I need to draw info from here as following: "QSO_DATE", "8", "20070909" so I use code:
sscanf("QSO_DATE:8:D>20070909", "%s:%d:D>%d")
But returning array looks like this:
Array
(
[0] => QSO_DATE:8:D>20070909
[1] =>
[2] =>
)
What is wrong? maybe there is more efficient way to parse bunch of records like these:
<CALL:7>EM200FT<QSO_DATE:8:D>20140324<TIME_ON:4>1657<BAND:3>12M<MODE:5>PSK63<RST_SENT:3>599<RST_RCVD:0><QSL_SENT:1>Y<QSL_SENT_VIA:1>E<APP_EQSL_AG:1>Y<GRIDSQUARE:6>KN45kj<EOR>
<CALL:5>9V1SV<QSO_DATE:8:D>20140328<TIME_ON:4>1019<BAND:3>10M<MODE:4>JT65<RST_SENT:6>VK4CMV<RST_RCVD:0><QSL_SENT:1>Y<QSL_SENT_VIA:1>E<QSLMSG:54>Thank you and I confirm your SWL report, 73's de Siva.<APP_EQSL_AG:1>Y<GRIDSQUARE:6>OJ11ui<EOR>
<CALL:5>RA6DQ<QSO_DATE:8:D>20140328<TIME_ON:4>1019<BAND:3>10M<MODE:4>JT65<RST_SENT:3>599<RST_RCVD:0><QSL_SENT:1>Y<QSL_SENT_VIA:1>E<QSLMSG:3>73!<APP_EQSL_AG:1>Y<GRIDSQUARE:6>KN85nf<EOR>
%s means any characters, including colons, digits, chevrons, etc, except whitespace characters) and sscanf uses a greedy grab.... using more precise alternatives like %[A-Z_] or %[^:] might serve you better that %s
$result = sscanf("QSO_DATE:8:D>20070909", "%[^:]:%d:D>%d");
var_dump($result);
Which uses %[^:] to scan for any character other than a :

PHP Regex search failing when adding a second capture group

I have the following named capture group that works exactly as intended. It grabs the last date/time of a specific format from a string of text.
$re = "/.*(?<date>[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+:[0-9]{1,2} (AM|PM))/s";
I want to capture the user ID that follows so I changed it to the following
$re = "/.*(?<date>[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+:[0-9]{1,2} (AM|PM)) (?<name>\\w+)/s";
However when I do so it breaks both values giving the following error
Notice: Undefined index: date in Q:\XAMPP\htdocs\index.php on line 272
The Error stems from the preg_match matches array being blank. Print_r confirms the array does not contain any information once the regex is changed to the second value.
Both of these work fine in external sites as the link below to Regex101 shows
http://regex101.com/r/zO0mK0/4
Using PHP 5.5.9
So the question is, am I missing something in this regex statement that is breaking it between the external site and my internal code or does this work meaning it is 100% purely my php that is causing this issue.
$LastDateRegex = "/.*(?<date>[0-9]+\\/[0-9]+\\/[0-9]+ [0-9]+:[0-9]+:[0-9]{1,2} (AM|PM))/s";
preg_match($LastDateRegex, $arr2['WorkLog'], $LastDateMatches);
$Modsecs = (strtotime($ts) - strtotime($LastDateMatches['date']))%60;
This is an example of the code being used. As mentioned above, I know the error stems from the $LastDateMatches array being empty for the second regex example, however the code works 100% with the first so there is something between the two that causes the issue.
You have an extra \ here: \\w+ and \\ at a few places, if that makes a difference?
Not quite sure what you want returned but runnning this regular expression, where $str is the text you have in your regex101-link,
$regex = "/.*(?<date>\d+\/\d+\/\d+ \d+:\d+:\d{1,2} (?:AM|PM)) (?<name>\w+)/s";
preg_match($regex, $str, $LastDateMatches);
Output,
Array
(
[0] => ...
[date] => 6/20/2014 10:04:32 PM
[1] => 6/20/2014 10:04:32 PM
[name] => ihugett
[2] => ihugett
)

Extracting e-mail address from a html structure using PHP

I am trying to modify a php file (It is of Joomla extension Community Builder 1.9.1, and the file is \components\com_comprofiler\plugin\templates\default\default.php), in order to extract the e-mail address from a variable.
For description’s sake, let’s say this variable is $html. To make sure this variable is the right one containing the e-mail address that I'm targeting, I insert:
<pre><?php print_r($html) ?></pre>
Into the file, and its output is the email address with a mailto link, and the corresponding HTML is something like
<span id="cbMa47822" class="cbMailRepl">myemail#yahoo.com</span>
So I guess I can use:
<?php $html_array = explode("\"",$html);echo $html_array[5]; ?>
Io get 'mailto:myemail#yahoo.com'; But actually it only returns a notice of:
undefined offset:5
So I print_r($html_array), and it return something like
Array
(
[0] => cbMa14768
[2] => class=
[3] => cbMailRepl
[4] => >...
)
It looks like the <a> tag part of the html output is replaced by "...", like what you see in Chrome’s developer tool html inspector, where before you expand it, the HTML looks like:
<span id="cbMa47822" class="cbMailRepl">...</span>
I looked deeper into the php code, trying to find out how this $html is contructed, but it is totally beyond my understanding.
For learning purpose, my questions are:
why there is no [1] in the result of print_r($html_array)
How do I test a variable’s value more exactly, by more exactly I mean totally without html input, like if the value is "foo", if should display the HTML as is, but not a link (when I use print_r, it returns a link)?
And most importantly, based on the information given above, can you give my any hint regarding how I can extract the e-mail address from a variable like this?
Finally, for those who are willing to take a deeper look into this, the variable I am talking about is $this->tableContent[$userIdx][1][6]->value in \components\com_comprofiler\plugin\templates\default\default.php, originally it wasn't in the code but I did some test and confirm it contains the email address. I inserted the following code between line 450 & 451
<?php $html_array = explode("\"",$this->tableContent[$userIdx][1][6]->value);echo $html_array[5]; ?>
To extract an e-mail address from an HTML strcuture as you describe, just use regex and preg_match:
$html = '<span id="cbMa47822" class="cbMailRepl">myemail#yahoo.com</span>';
preg_match("/mailto:(.*)\">/is", $html, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>';
The output would be:
Array
(
[0] => mailto:myemail#yahoo.com">
[1] => myemail#yahoo.com
)
So to access that e-mail address, just do this:
echo $matches[1];
The output would be:
myemail#yahoo.com
To avoid links you can use escape sequence.
you can use regular expression to match if the given string matches the email address pattern and print it
PHP has a vast support for functions which can perform wierdest tasks so search for them

PHP Replace tags / placeholders / markers in text string with dynamic values

Basically, what I want to achieve is dynamically replace {SOME_TAG} with "Text".
My idea was to read all tags like {SOME_TAG}, put them into array.
Then convert array keys into variables like $some_tag, and put them into array.
So, this is how far I got:
//Some code goes here
$some_tag = "Is defined somewhere else.";
$different_tag = 1 + $something;
Some text {SOME_TAG} appears in different file, which contents has been read earlier.
//Some code goes here
preg_match_all('/{\w+}/', $strings, $search);
$search = str_replace(str_split('{}'),"",$search[0]);
$search = array_change_key_case( array_flip($search), CASE_LOWER);
...some code missing here, which I cant figure out.
Replace array should look something like this
$replace = array($some_tag, $different_tag);
//Then comes replacing code and output blah blah blah..
How to make array $replace contain variables dynamically depending on $search array?
Why not something along the lines of:
<?php
$replace = array(
'{TAG_1}' => 'hello',
'{TAG_2}' => 'world',
'{TAG_3}' => '!'
);
$myString = '{TAG_1} {TAG_2}{TAG_3}{TAG_3}';
echo str_replace(array_keys($replace), array_values($replace), $myString);
If I understand correctly:
You're working on trying to create a customizable document, using {TAGS} in order to represent replaceable areas that can be filled in with dynamic information. At some point in time while replacing the {TAGS} with the dynamic information, you want the dynamic information to be stored in automatically generated basic variable names, as $tags.
I'm not sure why you want to convert these tags to basic variables instead using them entirely as array keys. I would like to point out that this represents a security or functionality hole - what happens if someone puts {REPLACE} in as a tag in your document? Your replace array would get overwritten with dynamic data, and your whole program would fall apart. Either that, or the whole replace array would get dumped in for {REPLACE}, making for a very messy document with perhaps data you don't WANT them to have in it. Perhaps you have this dealt with - I don't have all the context here - but I thought I'd point out the risk factor.
As for a better solution, unless there's some specific need that you're addressing by going through $tags instead of using using the $replace array directly, I like #Emissary's answer.

Categories