2017-06-13 4 views
1

I'm doing a small application that will parse a PDF file and return some content.PHP : strpos don't return result with apostrophe (')

Here is a part of the content of the pdf file

Nous publions ci-dessous par profession les candidat-e-s qui ont réussi l'examen partiel, l'examen de fin d'apprentissage, l'examen de maturité professionnelleou obtenu le titre par validation des acquis.Cette liste est complétée au fur et à mesure de la réception des résultats.Les candidat-e-s en situation d'échec sont avisé-e-s personnellement et ils/elles ne sont pas mentionné-e-s ci-dessous.AGENT-E D'EXPLOITATION CFCAGENT-E EN INFORMATION DOCUMENTAIRE CFCAIDE EN SOINS ET ACCOMPAGNEMENT AFP

There are job name (AGENT-E D'EXPLOITATION CFC, AGENT-E EN INFORMATION DOCUMENTAIRE CFC,...)

I've took all the jobs and made an array with them, something like 135 jobs. And then, I'd like to do a strpos() to get the starting position of the job. Problem, It didn't return me the job containing an apostrophe.

Here's a part of my code

echo "METIER 1 : " . strpos($texte, 'AGENT-E D\'EXPLOITATION CFC') . "</br>"; 
echo "METIER 1.2 : " . strpos($texte, "AGENT-E D'EXPLOITATION CFC") . "</br>"; 
echo "METIER 2 : " . strpos($texte, "AGENT-E EN INFORMATION DOCUMENTAIRE CFC") . "</br>"; 

And here is the result

METIER 1 :

METIER 1.2 :

METIER 2 : 458

EDIT : Problem solved, the output from the pdf parser was corrupted, I just had to do a str_replace() and it's all good now !

Thank you everyone.

+0

You should dump the text and copy the '.Certains 'ne sont pas les mêmes que sur le clavier anglais. Esp si vous avez copié le texte à partir du Web ou du pdf, ils ne sont souvent pas le 'comme vous tapez sur le clavier anglais. – TurtleTread

+0

http://sandbox.onlinephpfunctions.com/code/f4793627fd02815c0dd5f5a43fa2f14ea7ca5856 fonctionne pour moi. Assurez-vous que vous obtenez effectivement des guillemets simples et pas un équivalent unicode qui n'est pas vraiment une seule citation au sens ASCII – apokryfos

+0

J'ai essayé de faire un var_dump ($ texte) et copiez le '. Ne fonctionne pas Copiez le 'du pdf original, ne fonctionnant pas, mettant le' à la main, ne fonctionnant pas. Je travaille avec un clavier suisse, en utilisant https://github.com/smalot/pdfparser pour analyser le pdf. – Anywen

Répondre

0

This works, using a double quoted string instead of a single quouted string with escapes

echo "METIER 1 : " . strpos($texte, "AGENT-E D'EXPLOITATION CFC") . "</br>"; 
echo "METIER 1.2 : " . strpos($texte, "AGENT-E D'EXPLOITATION CFC") . "</br>"; 
echo "METIER 2 : " . strpos($texte, "AGENT-E EN INFORMATION DOCUMENTAIRE CFC") . "</br>"; 

Results

METIER 1 : 408 
METIER 1.2 : 408 
METIER 2 : 434 

This also works for me

echo "METIER 1 : " . strpos($texte, 'AGENT-E D\'EXPLOITATION CFC') . PHP_EOL; 
echo "METIER 1.2 : " . strpos($texte, "AGENT-E D'EXPLOITATION CFC") . PHP_EOL; 
echo "METIER 2 : " . strpos($texte, "AGENT-E EN INFORMATION DOCUMENTAIRE CFC") . PHP_EOL; 

Result

METIER 1 : 408 
METIER 1.2 : 408 
METIER 2 : 434 
+0

Mon exemple n'est pas bien aligné, mais comme vous pouvez le voir dans METIER 1 et METIER 1.2, c'est la même chaîne mais écrite avec les 2 options. EDIT: Je vois que cela fonctionne pour vous, mon php 7.0.6 pourrait créer ce problème? Ou est la chaîne qui sort de l'analyseur pdf faux? – Anywen

+0

Semble fonctionner pour moi de toute façon – RiggsFolly

+0

Peut-être que vous avez une corruption mineure dans votre fichier de code. – RiggsFolly

1

I've used the file_put_contents() to write the content of my $texte variable (the one coming out of the pdfparser) into a .txt file. After opening it, the problem was clear. It had replaced all the ' with an alphanumerical value.

I couldn't see the problem from firefox because it was interpreting this value and showed me a '.

I just had to do a str_replace() on my string and it's all good.

Thank you everyone for the help !