How to search and delete string in php - php

I am attempting to use an RSS feed from Twitter (the ones that will be gone a year from now) with Feedburner but a line of code in the xml is causing a parsing error in feedburner. I think it's
xmlns:twitter="http://api.twitter.com"
I am looking for a way to give this information to feedburner in a way that it likes: namely php including the contents of the RSS xml page in an xml on my own site (with the offending string removed). Including the xml contents is a piece of cake - all I need is a piece of code to say iff string fragment == xmlns:twitter="http://api.twitter.com" then string frament = "".
Anybody any idea of the syntax for this?

You can try with str_replace:
str_replace('xmlns:twitter="http://api.twitter.com"', '', $string);
Also, you can try to urlencode http://api.twitter.com.

Alternatively, you could use preg_replace function:
http://php.net/manual/en/function.preg-replace.php
<?php
$string = 'string to be searched';
$pattern = 'a particular pattern';
$replacement = ''; //deletion
echo preg_replace($pattern, $replacement, $string);
?>

Related

php - Replace Template Code with HTML

I got a Button, that creates this template code
[5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)
The filename and file size vary und the user can change the ##LINKNAME##, as well.
This code goes to a database and when I get it back, I want to replace it to
##LINKNAME## <i>(96.51 kb)</i>
I think I need to use preg_replace() but I am not really good at regular expressions.
I stopped here:
<?php
$string = ' [5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)';
$regex = '[[a-zA-Z0-9]+.pdf](.*?)\s';
$replace = 'I DONT KNOW';
echo preg_replace($regex, $replace, $string);
?>
I know that this is a complete mess, but I'm not getting any results as long as I don't know the regex and the correct $replace.
Regex: ^\[([^\]]+)\](\w+)\s\(([^)]+)\)$
Replace with: \2<i>(\3)</i>
Demo

PHP regex replace between wordpress shortcode tag

I have a shortcode which I want to be able to strip away depending on the context of the post. Eg.
[tooltip slug="test"]Test Text[/tooltip]
I would like the output to be:
<span class="dummy">Test Text</span>
I have experimented (a lot!) with preg_replace and I can't seem to get it to recognize that the replacement string is between the ']' and then delimited by '[/tooltip]' without doing multiple passes.
Ideas?
Update: As so often happens, about 10 seconds after I wrote this one of my attempts seemed to work. I don't think it's as good as the solution below but FWIW...
$my_var .= preg_replace('/(?:\[tooltip slug=\"([^\"]*)"[^\>]*\]([^\<]*)\[\/tooltip\])/', '<span class="dummy">\\2</span>', $my_post->post_content);
Here is the simple regex you are looking for.
$result = preg_replace('%\[tooltip slug="[^"]*"]([^[]*)\[/tooltip]%',
'<span class="dummy">\1</span>', $subject);
What we do here is capture the text between the tooltip tags, and insert it in the replacement.
Let me know if you need any details.
$test = preg_match('/\[([^\]]+)\]([^\[]+)\[/', '[tooltip slug="test"]Test Text[/tooltip]', $matches);
echo $matches[2];

How can I parse in PHP to format user-submitted posts?

I want to give users some formatting options like Reddit or stackOverFlow do, but want to keep it in PHP. How can parse a string in PHP such that it recognizes patterns like **[anything here]**?
explode() doesn't seem to solve the problem as elegantly as I'd like. Should I just use nested ifs and fors based on explode()'s output? Is there a better solution here?
This has already been done countless times by others, I'd recommend using existing libraries.
For example, you can use Markdown: http://michelf.com/projects/php-markdown/
Check regular expressions:
$string = '*bold* or _underscored_ or even /italic/ ..';
// italic
$string = preg_replace('~/([^/]+)/~', '<i>$1</i>', $string);
// bold
$string = preg_replace('/\*([^\*]+)\*/', '<b>$1</b>', $string);
// underscore
$string = preg_replace('/_([^_]+)_/', '<u>$1</u>', $string);
echo $string;
output:
<b>bold</b> or <u>underscored</u> or even <i>italic</i> ..
or use a BBCode parser.

Simplest way to convert all html links in a string using PHP

I am trying to convert a block of text that contains html text - i'd like to find all http links and convert them for link tracking purposes.
So eg anything like this in a string would be converted to the latter
Some Link
Some Link
Can anyone how to do this taking into account the original string will consists of all sorts of html, images etc..
Use this regex : (UPDATED)
<?php
$str = '<h1>Page Title</h1>Google';
$text = preg_replace("/href=\"http\:\/\/([a-zA-Z0-9\-]+\.[a-zA-Z0-9]+\.[a-zA-Z]{2,3}(\/*)?)/","href=\"http://www.mysite.com/tracking.php?url=$1\"",$str);
echo $text;
?>
Outputs :
<h1>Page Title</h1>Google
$str = '<h1>Page Title</h1>Google';
$text = preg_replace('href=\"http\://([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?)\"', 'href=\"YOUR_TRACKING_URL=$1\"', $str);
echo $text;
Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /home/....
(sorry for the duplication)

Highlite words from searchstring

I wrote a little search script for a client, it works and words get highlited, BUT...
Imagine this situation:
search term: test
found result: Hello this is a test
In this example both 'test' in the href part and between the <a> tags get highlited, breaking the link.
How could I prevent this?
Edit:
So this is what I need: A regex replace function that replaces all matched search strings EXCEPT the ones that are located inside a href attribute
You can not parse XML with regular expressions. :( If you want a dirty regex solution that still works in many cases you may try this regex.
">[^<]*?(test)"
First you look for a tag closing brace and than you make sure that no other tag is opened in between.
Ideally you want to parse HTML and replace only the textual parts of it.
Got it!
$body = $row['body'];
$pattern = "/".$search_string."(?!([^<]+)?>)/i";
$replacement = "<strong class='highlite'>".$search_string."</strong>";
$altered_body = preg_replace($pattern, $replacement, $body);
print($altered_body);

Categories