PHP to remove xml [duplicate] - php

This question already has answers here:
How to get the value of an attribute from XML file in PHP?
(4 answers)
Closed 6 years ago.
I need some help with a problem if possible.
<IndexFile index="dlc:blahblahblahblah.zip" version="1.19.0" />
How would I use php to remove everything in the above line of code except
blahblahblahblah.zip
Note: blahblah isn't actual name, the name changes; I just need to remove the xml on either side of the .zip file.
I've tried a few things like strip_tags() but nothing works up to now.

Your test string was
<IndexFile index="dlc:blahblahblahblah.zip" version="4.19.0" />
Your regex pattern should be : index="dlc:(.+?)".
Your answer is:
blahblahblahblah.zip
Try it out at https://regex101.com/
See this answer for greedy vs nongreedy matching: java-pattern-does-not-return-leftmost-match

Ideally, you have to use an XML parser to parse the file and use XPaths/looping to get that item.
If that is the only line, why can't you just use a Regex to extract the value? index="[a-zA-Z0-9_.-:]*" (or [a-zA-Z0-9_.-:]*.zip)?
Again, you need to think about the future impacts.

Related

find/replace in PHP string [duplicate]

This question already has answers here:
PHP strtr vs str_replace benchmarking
(3 answers)
Replace text in a string using PHP
(3 answers)
Closed 5 years ago.
I have a PHP string in which I would like to find and replace using the strtr function, problem is I have variable fields so I won't be able to replace by name. The string contains tags like the following:
[field_1=Company]
[field_4=Name]
What makes it difficult is the "Company" and "Name" part of the "tag", these can be variable. So I basically looking for a way to replace this part [field_1] where "=Company" and "=Name" must be discarded. Can this be done?
To explain: I'm using "=Company" so users don't just see "field_1" but know the value it represents. However users are able to change the value to what they see fit.
You are probably looking for regular expressions. There is a function in PHP to do a regex replace:
http://php.net/manual/en/function.preg-replace.php
Been a while since I've worked in PHP but you might want to try something like this:
preg_replace('/field_\d/','REPLACEMENT','[field_1=Company]');
Should result in
[REPLACEMENT=Company]
If you want to replace everything except the brackets:
preg_replace('/field_\d+=\w+/','REPLACEMENT','[field_1=Company]');

How to ignore a part of a pattern to use preg_match_all [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I'd like to ignore what's inside a href element and make the function search with the rest of the pattern, I'd like it to be something like this:
preg_match('|<tr><td class="even">(.*?)</td>|', $content, $value)
Originally the href has /?tab=episode&seriesid=121361&seasonid=364731&id=3436411&lid=16 but it's something I'd like the function to ignore.
EDITED:
I'd like to match the 2 from <td class="even">2</td> but not just 1, i'll match more later and they have differents href values so i want a regex that means this href maybe variable. I don't know if i explained well, my english is not very good.
If you use <a href="[^"]*"> it will match anything that isn't a " character.
Ideally you want to use a dom parser, See this post. SimpleXML with XPath is a good tool.
I believe you are searching for the following regex:
<tr><td class="even">(.*)</td>
This will match all of these:
<tr><td class="even">2</td>
<tr><td class="even">1</td>
<tr><td class="even">Hallo</td>
<tr><td class="even">Test</td>
I Also kept your (.*) as I am assuming you are using that group later.
If you want to search for a specific value in the a-tag content just replace (.*)with that.

Regex for PHP: match a character within a set of quotes [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
<input name="so and so" id="john doe" value="something something" />
I want to match only the space character that is within the " of a value= attribute.
I came up with this:
value="[^"',:<>/\r\n]*[ ][^"',:<>/\r\n]*"
Problem is this matches the whole string, where I just need the space in the matched string from above.
I am reading some tutorials, which leads me to think I need to use a conditional statement, but I can't seem to figure out how that would work. My guess (which of course didn't work) was:
(?(?=value="[^"',:<>/\r\n]*[^"',:<>/\r\n]*")[ ])
Any help is appreciated. Thanks!
\bvalue="\K|\G(?!^)[^"]*?\K\s+
You can use this to find spaces which are in value=" and before closing ".See demo.
https://regex101.com/r/lR1eC9/11
EDIT:
(?:(?<=\bvalue=")|\G(?!^))[^"]*?\K\s+(?=[^"=]*")
See demo.
https://regex101.com/r/lR1eC9/13

Strip RTF strings with PHP - Regex [duplicate]

This question already has answers here:
Regular Expression for extracting text from an RTF string
(11 answers)
Closed 9 years ago.
A column in the database I work with contains RTF strings, I would like to strip these out using PHP, leaving just the sentence between.
It is a MS SQL database 2005 if I recall correctly.
An example of the kind of strings pulled from the database (need any more let me know, all the rest are similar):
{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Tahoma;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.\lang2057\fs17\par
}
I would like this to be stripped to only return:
ASSEMBLE COMPONENTS AS DETAILED ON DRAWING.
Now, I have successfully managed to strip the characters in ASP.NET for a previous project, however I would like to do so using PHP. Here is the regular expression I used in ASP.NET, which works flawlessly may I add:
"(\{.*\})|}|(\\\S+)"
However when I try to use the same expression in PHP with a preg_replace it does not strip half of the characters.
Any regex gurus out there?
Use this code. it will work fine.
$string = preg_replace("/(\{.*\})|}|(\\\S+)/", "", $string);
Note that I added a '/' in the beginning and at the end '/' in the regex.

PHP: Regular Expression to get a URL from a string [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Identifying if a URL is present in a string
Php parse links/emails
I'm working on some PHP code which takes input from various sources and needs to find the URLs and save them somewhere. The kind of input that needs to be handled is as follows:
http://www.youtube.com/watch?v=IY2j_GPIqRA
Try google: http://google.com! (note exclamation mark is not part of the URL)
Is http://somesite.com/ down for anyone else?
Output:
http://www.youtube.com/watch?v=IY2j_GPIqRA
http://google.com
http://somesite.com/
I've already borrowed one regular expression from the internet which works, but unfortunately wipes the query string out - not good!
Any help putting together a regular expression, or perhaps another solution to this problem, would be appreciated.
Jan Goyvaerts, Regex Guru, has addressed this issue in his blog. There are quite a few caveats, for example extracting URLs inside parentheses correctly. What you need exactly depends on the "quality" of your input data.
For the examples you provided, \b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$] works when used in case-insensitive mode.
So to find all matches in a multiline string, use
preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&##\/%=~_|$?!:,.]*[A-Z0-9+&##\/%=~_|$]/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Why not try this one. It is the first result of Googling "URL regular expression".
((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))+[\w\d:##%\/;$()~_?\+-=\\\.&]*)
Not PHP, but it should work, I just slightly modified it by escaping forward slashes.
source

Categories