I have problem with using get_file_content after preg_match and str_replace.
When I read from base, text look like:
Some text with shortcodes: [formularz {"agree":"1,1,1","option":"firstname,lastname,phone,email,company","leadfrom":"Allegro"}] [formularz {"agree":"0,1,0","option":"firstname,lastname,phone","leadfrom":"Allegro"}]
Then I looking my regular '[]':
preg_match_all('/\[formularz (.*)\]/',$campaign['content'],$matches);
Then I replace some chars and words:
echo str_replace($matches[0],str_replace([':','","','"','{','}'],['=','&','','file_get_contents("http:// mydomain.com/form.php?id_added_to_lead='.$id_added_to_lead.'&','")'],$matches[1]),str_replace(array("\r\n", "\r", "\n"), "",stripslashes($campaign['content']))) ;
And finally I have:
Some text with shortcodes:
file_get_contents("http:// mydomain.com/form.php?id_added_to_lead=100&agree=1,1,1&option=firstname,lastname,phone,email,company&leadfrom=Allegro")
file_get_contents("http:// mydomain.com/form.php?id_added_to_lead=100&agree=0,1,0&option=firstname,lastname,phone&leadfrom=Allegro")
What should I do, If I want to execute file_get_content in content?
Related
I have a large block of HTML that contains multiples URLs with spaces in them. How do I used Regex to replace any space that occurs in a URL, with a '%20'. The good thing is that all of the URLs end with '.pdf'.
Looking for something I could run in BBedit/Text Wrangler, or even PHP.
Example: http://www.site-name.com/dir/file name here.pdf
Need to return: http://www.site-name.com/dir/file%20name%20here.pdf
Instead of Regex you could use could use urlencode in PHP to achieve this which escapes the url for you. Similar to encodeURI in JavaScript.
I was faced with exactly the same problem. I solved it with this:
$text = preg_replace("/http(.*) (.*)\.pdf/U", "http$1%20$2.pdf", $text);
This looks for a space between http and pdf and then replaces the space with %20.
If your URLs have multiple spaces, then simply run the code over and over until all the spaces are gone:
while(preg_match("/http(.*) (.*)\.pdf/U", $text))
{
$text = preg_replace("/http(.*) (.*)\.pdf/U", "http$1%20$2.pdf", $text);
echo('testing testing');
}
However, I've found this will overwrite text if there are two or more URLs on the same line. I haven't found a solution for this yet.
I'm testing a preg_replace function, and I return from an ajax function the processed data (after I process the data through preg_replace, I put it through htmlentities() ):
My test string is:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
I'm trying to make sure all those characters aren't replaced. My replace function is:
$string = preg_replace('/[^a-zA-Z0-9\s+\n\r,.\/~!##\$%\^&*()\+={}\[\]|:;<>?\'"-]/', '', $string);
I return both the data from "echo" and after going through htmlentities() to see the difference.
when I return the data using alert(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
respectively. However, when I put either of those into $("#div").html(data), I get:
pr eg123 ~!##$%^&*()-+={}|[]:;< >? "...,'/...
so the multiple spaces are lost. Why does the .html() function reduce the spaces? And how can I fix this? Thanks
remove "\s+" from your regular expression and try again
"I'm trying to make sure all those characters aren't replaced." you mean it?
so i make a test like below:
$string = "~!##$%^&*()-+={}|[]:;< >?";
// $string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
echo "'", $string, "'";
output is
'~!##$%^&*()-+={}|[]:;< >?'
if you want keep whole white space between "<" and ">" in $string, i can say that no way, if you wanna output the same white spaces as you input: there are two way can make this:
1> use <pre> tage
2> use replace the white space
does you want these? if you want to keep all? why use regular?
I am working with this daily data feed. To my surprise, one the fields didn't look right after it was in MySQL. (I have no control over who provides the feed.)
So I did a mysqldump and discovered the zip code and the city for this record contained a non-printing char. It displayed it in 'vi' as this:
<200e>
I'm working in PHP and I parse this data and put it into the MySQL database. I have used the trim function on this, but that doesn't get rid of it. The problem is, if you do a query on a zipcode in the MySQL database, it doesn't find the record with the non-printing character.
I'd like the clean this up before it's put into the MySQL database.
What can I do in PHP? At first I thought regular expression to only allow a-z,A-Z, and 0-9, but that's not good for addresses. Addresses use periods, commas, hyphens and perhaps other things I'm not thinking of at the moment.
What's the best approach? I don't know what it's called to define it exactly other than printing characters should only be allowed. Is there another PHP function like trim that does this job? Or regular expression? If so, I'd like an example. Thanks!
I have looked into using the PHP function, and saw this posted at PHP.NET:
<?php
$a = "\tcafé\n";
//This will remove the tab and the line break
echo filter_var($a, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW);
//This will remove the é.
echo filter_var($a, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
?>
While using FILTER_FLAG_STRIP_HIGH does indeed strip out the <200e> I mentioned seen in 'vi', I'm concerned that it would strip out the letter's accent in a name such as André.
Maybe a regular expression is the solution?
You can use PHP filters: http://www.php.net/manual/en/function.filter-var.php
I would recommend on using the FILTER_SANITIZE_STRING filter, or anything that fits what you need.
I think you could use this little regex replace:
preg_replace( '/[^[:print:]]+/', '', $your_value);
It basically strip out all non-printing characters from $your_value
I tried this:
<?php
$string = "\tabcde éç ÉäÄéöÖüÜß.,!-\n";
$string = preg_replace('/[^a-z0-9\!\.\, \-éâëïüÿçêîôûéäöüß]/iu', '', $string);
print "[$string]";
It gave:
[abcde éç ÉäÄéöÖüÜß.,!-]
Add all the special characters, you need into the regexp.
If you work in English and do not need to support unicode characters, then allow just [\x20-\x7E]
...and remove all others:
$s = preg_replace('/[^\x20-\x7E]+/', '', $s);
Currently, I'm using the following code:
preg_replace('/\s+/m','<br>',$var);
to replace line ends with <br> elements. An example of what I want:
Text Text Text
Text Text Text
Text Text Text
Should end up being:
Text Text Text<br>Text Text Text<br><br>Text Text Text
This does what it needs to, but I'd like to recognize when there is a double space and add two breaklines, instead of a single one. How can I do this, while retaining the current effect for single break lines?
I'm not too familiar with how preg_replace() works and I actually had to get help here to get that function in the first place. I took a look in the PHP manual and the function seemed a little confusing. Would anyone know of a site where I could learn how it works correctly?
You can simply replace each end-of-line character with <br />:
$var = str_replace(array("\r\n", "\n"), '<br />', $var);
There is no need to use a regular expression, but if you really want, you can use preg_replace to achieve the same effect:
$var = preg_replace("/\r?\n/", '<br />', $var);
you can do this by adding the g-modifier to the preg_replace like so:
preg_replace('/\s+/mg','<br>',$var);
preg stands for Perl Regular Expression - you'll find a lot more examples with this search string, e.g. this site or this site (I'm actually unsure, what the m-modifier does?)
Alternatively, you could use the simple $var = str_replace(' ', '<br', $var). I'm unsure, which one is faster.
Edit: If you want to replace newlines with html-breaks, use the nl2br() function.
php has a built in function for this
echo nl2br( $var );
this does \n, \r\n, \r, and \n\r
http://www.php.net/manual/en/function.nl2br.php
Check out this one. Not sure you are asking for that, but at least it works for me
$input = <<<DOC
Test Test Test
Test Test Test
Test Test Test
DOC;
$output = preg_replace("/$/m","<br/>",$input);
echo $output;
Hovewer, nl2br does just the same.
Is this what you’re trying to do?
<?php
$text = <<<EndText
Text Text Text
Text Text Text
Text Text Text
EndText;
$text = str_replace("\r", "", $text);
$text = str_replace("\n", "<br>", $text);
echo $text;
?>
I want to clean up some parsed text such as
\n the said \r\n\r\n\r\n I look in your eyes my dear\r\n\r\nI see green rolling Forests\r\n\r\nI see the far away Sky\r\n\r\nThey turn into the rain\r\n\r\n\r\nI see high soaring eagles... more\n
So I want to get rid of the "\n", "\r\n", "\r\n\r\n", "\r\n\r\n\r\n", "\r\n\r\n\r\n\r\n" and "\r". That's all the combinations that appear in my parsed text.
Is there a way to do this in php?
If you just want 1 newline instead of multiple I would suggest this:
$clean = preg_replace(array("/\r+/","/(\n){2,}/"),array("","\n"),$text);
Otherwise str_replace to strip out newlines or nl2br will do the job. You could also adapt the regex to replace 1 or more newlines with a BR tag:
$clean = preg_replace(array("/\r+/","/\n+/"),array("","<br />"),$text);
What about
$text = str_replace(array("\n", "\r"), '', $text);
That will remove all new line characters.
If you want them as new lines, I'd change the replace to <br /> for HTML (or better still, use PHP's nl2br()), or standardise them in normal text with \n, for example.