PHP "spinning" content via random find/replace - php

I'm using file_get_contents('mysourcefile.html') to load the contents of mysourcefile.html into mysql db.
I have two things that I want to do to the contents of mysourcefile.html before I insert it into the db.
First...
I'd like to do a find/replace on specific string matches contained in mysourcefile.
For example: the tags that a user may place in their source input files would look something like this:
Welcome to [site-name], located at
[site-url] contact us at [site-email]
if you need help.
And I'd like to do a simple string match replacement on these values as they appear in the source file before they are written to the db. The replacement text would come from the wordpress database setup fields. For example, get_option('admin_email') and get_option('home')
Secondly...
I'd also like to allow the user to specify, via a special bracket, a string of words in which to use in order to randomize the content each time its imported, using the same input source file.
For example, in the above sentence, it might be encoded in the source file like so:
I'd also [%like|prefer|want%] to
[%allow|permit%]the user to
[%specify|declare|select%] via a
[%special|unique|predefined%] bracket,
a string of [%words|characters|text%]
in which to use in order to randomize
the content from site to site, using
the same input source file.
So I want to parse that content string and do a simple random replacement of each set element to pick one word out of the collection and use that word for the insert.
Its basically a crude content replacement/spinner and I'm looking for some direction and methods which I could use to do it.

For the first part:
$tags = array('[site-name]', '[site-url'], '[site-email]');
$words = array("My Name", "My URL", "My Email");
$content = str_replace($tags, $words, $content);
The second part might be a little trickier. But the process is:
Grab the content between "[%" and "%]" tags.
implode("|", $string);
Pick a random value
So .. you'll need someone who knows Regex.

Related

Squares apperaing in text, want to search & replace

I have a site where a client has inserted text. Probably by copying it from a PDF or Word document.
There is a strange character that appears at random places in the inserted text.
The site is full of them so i would like to get rid of them programmatically, either by a search and replace in the database or by filtering via PHP when the value is printed out via str_replace();
The character looks like this, and is not recognized when i search for it in the database, and not even when you search for it with the built in browser search function.
It looks the same in the database as it does when rendered. In some cases it´s like an empty square, in some cases with a questionmark inside it.
I have tried this without luck:
$value = str_replace( '', '', $value );
(The square in first argument seems to be removed when i save)

PHP code to present query result present as html, and add hyperlink to text that is a url

I have a cell in a database $Data. It could contain any data. some of that data might contain a url.
Using mysql I need php to return the text out of the cell, hyperlinking anything that is a url. I found the preg_replace function elsewhere on Overflow but its not working.
Im trying to find code that will extract $Data then present $Data as text plus hyperlinked url.
I found this:
preg_replace('/\b(https?:\/\/(.+?))\b/', '\1', $text);
but a) its not working and b) I need a statement to extract $Data first
Live regex says it works: http://www.phpliveregex.com/p/aqB (click on preg_replace on the right).
$data = \preg_replace('/\b(https?:\/\/.+)\b/i', '\1', $data);

Regex replace matched subexpression (and nothing else)?

I've used regex for ages but somehow I managed to never run into something like this.
I'm looking to do some bulk search/replace operations within a file where I need to replace some data within tag-like elements. For example, converting <DelayEvent>13A</DelayEvent> to just <DelayEvent>X</DelayEvent> where X might be different for each.
The current way I'm doing this is such:
$new_data = preg_replace('|<DelayEvent>(\w+)</DelayEvent>|', '<DelayEvent>X</DelayEvent>', $data);
I can shorten this a bit to:
$new_data = preg_replace('|(<DelayEvent>)(\w+)(</DelayEvent>)|', '${1}X${2}', $data);
But really all I want to do is simulate a "replace text between tags T with X".
Is there a way to do such a thing? In essence I'm trying to prevent having to match all the surrounding data and reassembling it later. I just want to replace a given matched sub-expression with something else.
Edit: The data is not XML, although it does what appear to be tag-like elements. I know better than parsing HTML and XML with RegEx. ;)
It is possible using lookarounds:
$new_data = preg_replace('|(?<=<DelayEvent>)\w+(?=</DelayEvent>)|', 'X', $data);
See it working online: ideone

Using PHP to search a text file

I'm trying to create a code to download mp3 files embedded in a page. It starts out as a submit form. You input the URL and submit it, and it writes the HTML source of that page to a text file. I also set the script to search the source to see if there is an audio file embedded. I suppose I should include that it's not in the format of filename.mp3. The format is:
embed type="application/x-shockwave-flash" src="http://diaryofthedead.tumblr.com/swf/audio_player_black.swf?audio_file=http://www.tumblr.com/audio_file/1435664895/tumblr_lb2ybulZkt1qb5hrc&color=FFFFFF" height="27" width="207" quality="best"
So here's the thing, there's just a certain string you have to add to the end of the file, for it to redirect to the mp3 file. I know the string. What I want to do is extract, for example "http://www.tumblr.com/audio_file/1435664895/tumblr_lb3ybulZkt1q5hrc" from the middle of this. I know how to read from files but I have no idea how to extract certain parts from it without knowing the exact filename already. So is there any way I can have it search the source for "audio_file" and if it finds the string, extract the audio file?
If your program is just a parser for extracting MP3 files embedded in a webpage you don't even need to save the contents of the webpage onto a file, you can work with the page source inside just your server's memory.
If you want to detect paths to MP3 inside flashes, provided you know how does it match a regular expression, you are done.
If you don't know much about rgular expressions, you should look at them.
If you don't want as much power as a regular expression can give to you, you can always find strings by position, like:
$pos = strpos($haystack, $needle);
Beware: strpos() will find the first (strrpos will find the last) occurrence of a string. So you need to make it as explicit as you can, or you might end up capturing something unwanted.
Take a look at http://www.regular-expressions.info/quickstart.html or something similar.
I can't post more links because I don't have enough reputation yet
You can try using preg_match (http://php.net/manual/en/function.preg-match.php) to get the contents between "audio_file=" and "&".
Or you can also use a string between function to get the contents between those two strings:
http://www.php.net/manual/en/function.substr.php#89493

PHP tags. How to minimize the tags usage in php script

I am using an ask answer script on a website and it converts the headline title words into the search query tags automatically.
For example: "Who are you?" is converted into tags 'Who' 'are' and 'you' tags respectively. I want tags to be displayed only if the letters in the word are greater than 4. Is it possible?
I am not into php but I searched for the 'tags' in my script and have uploaded the result here http://pastebin.com/m670a1609. Kindly let me know which source file would help in achieving this..
Thanks!
I want tags to be displayed only if
the letters in the word are greater
than 4. Is it possible?
You can do like this:
if (strlen($your_word) > 4)
{
// go ahead
}
Surely it would be easier to directly ask the provider of the script for help. A quick Google search makes me believe that AnswerScript is a commercial package that comes with support.
Maybe you should be looking for something like
explode(' ', $title_words)
and not php tags. This function splits the variable $title_words into separate array elements using a space as a delimiter. More or less what that script does to that title.

Categories