How i can delete some data-attributes with value in file-content? - php

I want to delete some data-attributes with they values inside the sourcecode of a file.
I load the file inside a variable
$filecontent = file_get_contents($pathtofile);
Now I want to delete some code, for example
delete data-myval="something123"
Important to know is, that the value "something123" is dynamically.
How I can delete all matches of data-myval="find_all"
I tried to do it with str_replace and explode failed, it was to complicated and produced too long of code.
Is there a easier way to do it?
Thanks a lot.

You can achieve this using preg_replace. This will allow you to match any regex expressions and remove the code.
Your regex expression will be (see regex101):
data-myval\=\"[^"]+\"
Using preg_replace:
$filecontent = file_get_contents($pathtofile);
$filecontent = preg_replace('/data-myval\=\"[^"]+\"/', '', $filecontent);
After this you will need to save the file using file_put_contents().
file_put_contents($pathtofile, $filecontent);

Related

Automatically link words using PHP

I have thousands of PHP pages which have a header and footer included using php, like
<?php include("header.php"); ?> //STATIC CONTENT HERE <?php include("footer.php"); ?>
I want to implement auto keyword linking for certain keywords in the static text. But I can only add PHP codes to my header or footer files.
This can be a complex operation. The steps:
Get all the files whose words you want to replace (glob)
Specify an array (or arrays) for the "find" and "replace" criteria
Iterate over the files returned by glob replacing the text as you go (preg_replace)
Write the new text (file_put_contents)
This sample code replaces all words in the $words array with a link to http://www.wordlink.com/<yourword>. If you need a different link for each word you'll need to specify $replace as an array using $1 where you want the searched word to appear in the replacement (and change $replace in the regex to $replace[$i]).
Also, the glob function below looks for all html files in the specified $filesDir directory. If you need something different you're going to have to manually edit the glob path yourself. Finally, the regular expression used only replaces whole words. i.e. if you wanted to replace the word super, the word superman will not have the word super replaced in the middle.
Oh, and the replace is NOT case sensitive as per the i modifier at the end of the pattern.
// specify where your static html files live
$filesDir = '/path/to/my/html/files/';
// specify where to save your updated files
$newDir = '/location/of/new/files/';
// get an array of all the static html files in $filesDir
$fileList = glob("$filesDir/*.html");
$words = array('super', 'awesome');
$replace = '$1';
// iterate over the html files.
for ($i=0; $i < count($fileList); $i++) {
$filePath = $filesDir . $fileList[$i];
$newPath = $newDir . $fileList[$i];
$html = file_get_contents($filePath);
$pattern = '#\b(' . str_replace('#', '\#', $words[$i]) . ')\b#i';
$html = preg_replace($pattern, $replace, $html);
file_put_contents($newPath, $html);
echo "$newpath file written\n";
}
Obviously, you need write-access to the new folder location. I would not recommend overwriting your original files. Translation:
Always backup before doing anything crazy.
P.S. the regexes are not UTF-8 safe, so if you're dealing with international characters you'll need to edit the regex pattern as well.
P.P.S. I'm really being kind here because SO is not a code-for-free site. Don't even think about commenting something like "it doesn't work" when I try it :) If it doesn't fit your specifications, feel free to peruse the php manual for the functions involved.
This is just an idea. I did a quick test and seems works...
<?php
include("header.php");
ob_start();
?>
//STATIC CONTENT HERE
<?php
$contents = ob_get_contents();
ob_end_clean();
// now you have all your STATIC CONTENT HERE into $contents var
// so you can use preg_replace on it to add your links
echo $contents_with_my_links;
include("footer.php");
?>
Indeed you should add this code to your current header/footer files.
OK. Its just an idea that solves the problem. As rdlowrey said this may be inefficient, but if you need replace keywords dynamically (with database based link, for instance) then this could be a good solution...

How can I replace apostrophes while uploading a csv file?

I'm having trouble replacing apostrophes while uploading a csv file with a bunch of different descriptions.
Right now I have
$remarks = str_replace("'", "’", $data[28]);
This gives me an error starting with the first apostrophe that shows up in my file. That first phrase where the apostrophe appears ends in "'s". If I change it to
$remarks = str_replace("'s", "’", $data[28]);
it will go past that first problem and get to the next problem ('t).
What am I doing wrong? I'm new to php, and I'm sure this must be a simple solution...
array_map($data, function($a) { return(str_replace($a, "'", "’")) });
should walk all elements of the array and replace all quotes for you.
It looks like you're trying to re-invent the wheel. It looks like you're trying to parse the csv yourself. If you are stop it. You should be using str_getcsv and you won't have to worry about escaping anything.
After that, you'll probably want to look into preg_replace.
preg_replace( "#'\w?#g", '', $data[$index] );

PHP Extract Text from Webpage

Is it possible to do something with PHP where I can set up a connection to a URL like http://en.wikipedia.org/wiki/Wiki and extract any words that contain a prefix like "Exa" and "ins" such that the resulting PHP page will print out all the words that it found. For example with "Exa", the word "Example" would be printed out each time it found an instance of "Example". Same thing for words that start with "ins".
$data = strip_tags(file_get_contents($url));
$matches = array();
preg_match('/\bExa|ins([^\b]+)/', $data, &$matches);
for ($i = 1; $i < count($matches); $i++) {
echo "Match: '".$matches[$i]."'\r\n";
}
Probably something like this, though I'm not so sure about the regex, I haven't tested it yet...
Edit: I changed it, it should work now... (\B => \b and strip_tags to prevent HTML-classes from being matched).
I don't have a full answer with example to give you, but yes, you should be able to read the whole page into a string variable and then do normal string operations on it. It will read in all the HTML, so you will probably need to do a lot of regex to eliminate tags if you don't want them.
Read the page into a string using file_get_contents. Use one of the various string functions to examine the page.
Yes, this possible. A potential approach would be to:
Use something like fopen (if allow_url_fopen is enabled - failing that use CURL) to grab the external web page content.
Remove the (presumably not required) HTML tags via strip_tags.
Use strtok to tokenise and iterate over the remaining content, checking for whatever conditions you require.

regex to get current page or directory name?

I am trying to get the page or last directory name from a url
for example if the url is: http://www.example.com/dir/ i want it to return dir or if the passed url is http://www.example.com/page.php I want it to return page Notice I do not want the trailing slash or file extension.
I tried this:
$regex = "/.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*/i";
$name = strtolower(preg_replace($regex,"$2",$url));
I ran this regex in PHP and it returned nothing. (however I tested the same regex in ActionScript and it worked!)
So what am I doing wrong here, how do I get what I want?
Thanks!!!
Don't use / as the regex delimiter if it also contains slashes. Try this:
$regex = "#^.*\.(com|gov|org|net|mil|edu)/([a-z_\-]+).*$#i";
You may try tho escape the "/" in the middle. That simply closes your regex. So this may work:
$regex = "/.*\.(com|gov|org|net|mil|edu)\/([a-z_\-]+).*/i";
You may also make the regex somewhat more general, but that's another problem.
You can use this
array_pop(explode('/', $url));
Then apply a simple regex to remove any file extension
Assuming you want to match the entire address after the domain portion:
$regex = "%://[^/]+/([^?#]+)%i";
The above assumes a URL of the format extension://domainpart/everythingelse.
Then again, it seems that the problem here isn't that your RegEx isn't powerful enough, just mistyped (closing delimiter in the middle of the string). I'll leave this up for posterity, but I strongly recommend you check out PHP's parse_url() method.
This should adequately deliver:
substr($s = basename($_SERVER['REQUEST_URI']), 0, strrpos($s,'.') ?: strlen($s))
But this is better:
preg_replace('/[#\.\?].*/','',basename($path));
Although, your example is short, so I cannot tell if you want to preserve the entire path or just the last element of it. The preceding example will only preserve the last piece, but this should save the whole path while being generic enough to work with just about anything that can be thrown at you:
preg_replace('~(?:/$|[#\.\?].*)~','',substr(parse_url($path, PHP_URL_PATH),1));
As much as I personally love using regular expressions, more 'crude' (for want of a better word) string functions might be a good alternative for you. The snippet below uses sscanf to parse the path part of the URL for the first bunch of letters.
$url = "http://www.example.com/page.php";
$path = parse_url($url, PHP_URL_PATH);
sscanf($path, '/%[a-z]', $part);
// $part = "page";
This expression:
(?<=^[^:]+://[^.]+(?:\.[^.]+)*/)[^/]*(?=\.[^.]+$|/$)
Gives the following results:
http://www.example.com/dir/ dir
http://www.example.com/foo/dir/ dir
http://www.example.com/page.php page
http://www.example.com/foo/page.php page
Apologies in advance if this is not valid PHP regex - I tested it using RegexBuddy.
Save yourself the regular expression and make PHP's other functions feel more loved.
$url = "http://www.example.com/page.php";
$filename = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_FILENAME);
Warning: for PHP 5.2 and up.

php replace a pattern

Suppose in a file there is a pattern as
sumthing.c: and
asdfg.c: and many more.. with *.c: pattern
How to replace this with the text yourinput and save the file using php
The pattern is *.c
thanks..
You can read the contents of the file into a PHP string using file_get_contents, do the *.c to yourinput replacement in the string and write it back to the file using file_put_contents:
$filename = '...'; // name of your input file.
$file = file_get_contents($filename) or die();
$replacement = '...'; // the yourinput thing you mention in the quesion
$file = preg_replace('/\b\w+\.c:/',$replacement,$file);
file_put_contents($file,$filename) or die();
You can use PHP's str_replace or str_replace ( in case its a regex pattern). CHeck the syntax of these two functions and replace the *.c with your input.
.c pattern should be something like /?(.c)$/
First open file and get it's content:
$content = file_get_contents($path_to_file);
Than modify the content:
$content = preg_replace('/.*\.c/', 'yourinput');
Finally save the result back to the file.
file_put_contents($path_to_file, $content);
Note: You may consider changing the regexp because this way it match the '.c' string and everything before it. Maybe '/[a-zA-Z]*\.c/' is what you want.

Categories