Preg Replace Help for php string - preg-replace

$testString = 'this is testing start [youtube=http://www.youtube.com/watch?v=abcd&w=640&h=390] this is last';
I want this to be replace by following :
$testString = 'this is testing start <iframe src="http://www.youtube.com/watch?v=abcd&w=640&h=390"></iframe> this is last';
Kindly Help !

You can use strtr function.
try this:
$testString = 'this is testing start [youtube=http://www.youtube.com/watch?v=abcd&w=640&h=390] this is last';
$new_String = strtr($testString, array('[' => '<iframe src="', ']' => '"></iframe>'));

Related

PHP Remove specific line return

After i've done a preg_replace to remove the TAG 'TagToremove', i still have a line return, would you know if either i could remove it during the preg_replace
Or After and how ?
Thanks
<?php
$strip_list = array('TagToremove');
foreach ($strip_list as $tag)
{
$temp = preg_replace('/<\/?' . $tag . '(.|\s)*?>/', '', $temp);
}
?>
String before Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<TagToremove>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
</TagToremove>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
String after Preg_replace:
<CodeServiceTransport></CodeServiceTransport>
<PrixTotalCommande>100</PrixTotalCommande>
<Ligne>
<ll>hh</ll>
<Id>48</Id>
<SKU>autreID</SKU>
<Quantity>1</Quantity>
</Ligne>
<Meta-CodeActivite></Meta-CodeActivite>
<Meta-CodeEnseigne></Meta-CodeEnseigne>
Don't parse HTML with regex, use a DOM parser instead.
Nevertheless, for your specific problem, you have to add an optional linebreak in your regex like that:
$temp = preg_replace('~</?' . $tag . '[^>]*>\R?~i', '', $temp);
\R stands for any kind of linebreak (ie. \r or \n or \r\n)

what is issue in my preg_replace for BB CODE? anyone can help me

Friend! check my code first one is not working but second is working why?
1=>
$value1 = "[b]PHP 2014[/b]";
$new1 = "/(\[)([bB])(\])/e";
$v1 = preg_replace($new1,"'<'.'h1'.'>'",$value1);
echo $v1;
/* RESULT::
PHP 2014[/b]
2=>
$value2 = "<b>PHP 2014</b>";
$new2 = "/(<)([bB])([^>*]>)/e";
$v2 = preg_replace($new2,"'\\1'.'h1'.'\\3'",$value2);
echo $v2;
/* RESULT::
PHP 2014 */
Just move the asterisk out of the character class:
$new2 = "/(<)([bB])([^>]*>)/";
// here __^
Also remove the e modifier because it is obsolete.

inserting new line with OpenOffice::OODoc

I am having quite the issue creating a new line with this module and feel like I am just missing something.
my perl code looks like this:
use OpenOffice::OODoc;
my $name = "foo <br> bar";
$name=~s/<br>/\n/g;
my $outdir = "template.odt";
my $doc = ooDocument(file => $outdir);
my #pars = $doc->getParagraphList();
for my $p (#pars)
{
$doc->substituteText($p,'{TODAY}',$date);
$doc->substituteText($p,'{NAME}',$name);
...
Problem is when I open it in word or open office I have no newlines. Although if it open it in a text edit I have my new lines.. Any ideas of how to fix this?
Ok I figured it out, hopefully this will save someone hours of searching for the same thing. I added:
use Encode qw(encode);
ooLocalEncoding('utf8');
my $linebreak = encode('utf-8', "\x{2028}");
$doc->substituteText($p,'<br>', $linebreak);
So my final code looks like this:
use OpenOffice::OODoc;
use Encode qw(encode);
ooLocalEncoding('utf8');
my $linebreak = encode('utf-8', "\x{2028}");
my $outdir = "template.odt";
my $name = "foo <br> bar";
my $outdir = "template.odt";
my $doc = ooDocument(file => $outdir);
my #pars = $doc->getParagraphList();
for my $p (#pars)
{
$doc->substituteText($p,'{TODAY}',$date);
$doc->substituteText($p,'{NAME}',$name);
$doc->substituteText($p,'<br>', $linebreak);
...
Maybe not the best way to do things but it worked!
You could try and insert and empty para after the current one:
If the 'text' option is empty, calling this method is the equivalent
of adding a line feed.
This sequence (in a text document) inserts a linefeed immediately after paragraph 4. Replace 4 with current position.
$doc->insertElement
(
'//text:p', 4, 'text:p',
position => 'after',
text => '',
);

How to capitalize $2 in preg_replace?

I run a community site (built by hand using html, css, and php) for my wow guild. Recently, my members have demanded the ability to use tags like #username in their posts, which would then link to the users page. I already have this script, as listed below:
<?php
$string = '#user really?';
if (preg_match('/(^|\s)#([a-z0-9_]+)/i', $string))
{
$string = preg_replace('/(^|\s)#([a-z0-9_]+)/i', '#$2', $string);
echo $string;
}
else {
echo $string;
}
?>
This works fine, and I notice that the $2 is the user's name, but, since all user's names start with capital letters, the $2 needs to be capitalized, and not everyone does that. Is there a way to code it to automatically capitalize $2, or should I just tell users to remember to capitalize?
Thanx in advance,
Steven
use ucfirst ($2) to capitalize the first character
and strtoupper($2) if you want it all capital
I added more preg_replace and it now links to user no matter how u type name, as long as u have # symbol (#user, #USER, #User, #uSER, etc...). for anyone needing my solution, I will post it below:
<?php
$proper_str = "going to battle with #ADROK";
$proper_str = ucfirst(strtolower($proper_str));
$proper_str = preg_replace('/#[a-z]/e', 'ucfirst(strtoupper("$0"))', $proper_str);
$proper_str = preg_replace('/(^|\s)#([a-z0-9_]+)/i', ' #$2', $proper_str);
echo $proper_str;
?>
and if you change somethings around and make it:
<?php
$proper_str = "going to battle with e#mail.com";
if (preg_match('/(^|\s)#([a-z0-9_]+)/i', $proper_str))
{
$proper_str = ucfirst(strtolower($proper_str));
$proper_str = preg_replace('/#[a-z]/e', 'ucfirst(strtoupper("$0"))', $proper_str);
$proper_str = preg_replace('/(^|\s)#([a-z0-9_]+)/i', ' #$2', $proper_str);
echo $proper_str;
} else {
$proper_str = ucfirst("$proper_str");
echo $proper_str;
}
?>
Then it will leave email addresses alone and make sure first letter of post is capitalized!

fql_query returns "Array" instead of name

I am trying to create a simple fb app that retrieves the name of the user using fql_query.
Code :
require_once 'facebook.php';
$appapikey = 'xxxx';
$appsecret = 'xxxx';
$facebook = new Facebook($appapikey, $appsecret) ;
$user_id = $facebook->require_login();
$q = "SELECT name FROM user WHERE uid='$user_id'";
$name = $facebook->api_client->fql_query($q);
echo "Name : $name[0][name]";
Output:
Name : Array[name]
Can you tell me what's going wrong here?
Thanks!
Mistake is in this line:
echo "Name : $name[0][name]";
you are including $name[0][name] in quotes, this should be
echo "Name : {$name[0][name]}";
or
echo "Name : ".$name[0][name];
An FQL query always returns an array, even if there is only one item in the result. Think of the result as rows in a normal SQL query.
Any time that I use arrays or database/API calls in PHP I print_r($array_name) to see what was returned. So this:
$name = $facebook->api_client->fql_query("SELECT name FROM user WHERE uid=$user_id");
print_r($name);
Should return this:
Array
(
[0] => Array
(
[name] => First Last
)
)
Another thing, I never put tick marks around values/variables in FQL.
$q = "SELECT name FROM user WHERE uid=$user_id";
$name = $facebook->api_client->fql_query($q);
But when you print the value you should put single quotes around the reference to the named index (in this case, 'name') and you should not wrap it all in double quotes:
echo "Name : " . $name[0]['name'];